【发布时间】:2011-03-12 16:21:37
【问题描述】:
我正在使用 Beautiful soup 来抓取数据。 BS 文档指出 BS 应始终返回 Unicode,但我似乎无法获得 Unicode。这是一个代码sn-p
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?p>
如何从 BS 获取 Unicode 对象?
我真的,真的迷失了这个。有什么帮助吗?提前致谢。
【问题讨论】:
标签: python unicode character-encoding beautifulsoup