我想用Python抓取网站的内容。就像这样:使用Python编写数据抓取
Apple’s stock continued to dominate the news over the weekend, with Barron’s placing it on the top of its favorite 2013 stock list.
但随着错误结果打印出来:
Apple âs stock continued to dominate the news over the weekend, with Barronâs placing it on the top of its favorite 2013 stock list.
符号 “'” 无法显示,这里是我的代码:
#-*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import urllib
from lxml import *
import urllib
import lxml.html as HTML
url = "http://www.forbes.com/sites/panosmourdoukoutas/2012/12/09/apple-tops-barrons- 10-favorite-stocks-for-2013/?partner=yahootix"
sock = urllib.urlopen(url)
htmlSource = sock.read()
sock.close()
root = HTML.document_fromstring(htmlSource)
contents = ' '.join([x.strip() for x in root.xpath("//div[@class='body']/descendant::text()")])
print contents
f = open('C:/Users/yinyao/Desktop/Python Code/data.txt','w')
f.write(contents)
f.close()
然而,设置之后,printf的功能就没用了。为什么?我该怎么做? 我使用的是Windows,默认的编码方式是gbk。
你可以张贴在执行该刮的代码? –
你是如何印制这份声明的?请发布您执行的确切命令以打印声明。 Python中没有printf函数,是吗? – stackoverflowery
试试[Beautiful Soup](http://www.crummy.com/software/BeautifulSoup/) –