BeautifulSoup 没有给我 Unicode

2024-01-12

我正在使用 Beautiful soup 来抓取数据。 BS 文档指出 BS 应始终返回 Unicode,但我似乎无法获取 Unicode。这是一个代码片段

import urllib2
from libs.BeautifulSoup import BeautifulSoup

# Fetch and parse the data
url = 'http://wiki.gnhlug.org/twiki2/bin/view/Www/PastEvents2007?skin=print.pattern'

data = urllib2.urlopen(url).read()
print 'Encoding of fetched HTML : %s', type(data)

soup = BeautifulSoup(data)
print 'Encoding of souped up HTML : %s', soup.originalEncoding 

table = soup.table
print type(table.renderContents())

从页面返回的原始数据是一个字符串。 BS 将原始编码显示为 ISO-8859-1。我认为 BS 会自动将所有内容转换为 Unicode,那么为什么当我这样做时:

table = soup.table
print type(table.renderContents())

..它给了我一个字符串对象而不是Unicode?

我如何从 BS 获取 Unicode 对象?

我真的真的迷失了。有什么帮助吗?提前致谢。


您可能已经注意到,renderContent 返回(默认情况下)以 UTF-8 编码的字符串,但如果您确实想要表示整个文档的 Unicode 字符串,您也可以执行 unicode(soup) 或使用 unicode( soup.prettify(),“utf-8”)。

Related

  • 如何在 BeautifulSoup 中呈现 unicode 标签的内容? https://stackoverflow.com/questions/843227/how-to-render-contents-of-a-tag-in-unicode-in-beautifulsoup/843239#843239
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BeautifulSoup 没有给我 Unicode 的相关文章

随机推荐