python - 使用 Python 对抓取数据进行编码

标签 python html-encode

我想用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。

最佳答案

首先,确保您知道 The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

其次,始终在内部使用 unicode。早解码,晚编码:当您废弃网站时,将其解码为 un​​icode 并在脚本内部将其处理为 unicode。否则,您的代码将在随机点崩溃,例如因为在某些中文网页的评论中遇到意外字符。仅当您稍后将其传递到某个地方(例如,某些可写流)时,您才应该对其进行编码(最好是“utf-8”)

三、使用BeautifulSoup 4

关于python - 使用 Python 对抓取数据进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927079/

相关文章:

python - 使用 z3py 查找集合中元素的最小数量

python - 为什么我在 Python 2.4 中使用 Unicode 数据时会出现 ASCII 编码错误,而在 2.7 中却不会?

python - 打印给定间隔的素数反转列表

python - 将 aiohttp 与多处理结合起来

asp.net - HTML 编码服务器端与客户端

c# - 编码/解码文本框

php - 如何在 PHP 5.3 中将表情符号转换为各自的 HTML 代码实体?

javascript - 如何在 javascript 中转义某些 html?

python - 如何使用 Pymongo 将两个字段的值传递到函数中?