BeautifulSoup 不给我 Unicode

Mri*_*lla 4 python unicode beautifulsoup character-encoding

我正在使用美丽的汤来抓取数据。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())
Run Code Online (Sandbox Code Playgroud)

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

table = soup.table
print type(table.renderContents())
Run Code Online (Sandbox Code Playgroud)

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

如何从 BS 获取 Unicode 对象?

我真的,真的很迷茫。有什么帮助吗?提前致谢。

Bru*_*oij 5

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

有关的