【发布时间】:2013-12-10 21:50:28
【问题描述】:
我正在运行一个获取 UTF-8 编码网页的 Python 程序,并使用 BeautifulSoup 从 HTML 中提取一些文本。
但是,当我将此文本写入文件(或在控制台上打印)时,它会以意外的编码写入。
示例程序:
import urllib2
from BeautifulSoup import BeautifulSoup
# Fetch URL
url = 'http://www.voxnow.de/'
request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8')
# Response has UTF-8 charset header,
# and HTML body which is UTF-8 encoded
response = urllib2.urlopen(request)
# Parse with BeautifulSoup
soup = BeautifulSoup(response)
# Print title attribute of a <div> which uses umlauts (e.g. können)
print repr(soup.find('div', id='navbutton_account')['title'])
运行它会给出结果:
# u'Hier k\u0102\u015bnnen Sie sich kostenlos registrieren und / oder einloggen!'
但我希望 Python Unicode 字符串将单词 können 中的 ö 呈现为 \xf6:
# u'Hier k\xf6bnnen Sie sich kostenlos registrieren und / oder einloggen!'
我尝试将 'fromEncoding' 参数传递给 BeautifulSoup,并尝试将 decode() 和 decode() 传递给 response 对象,但它要么没有任何区别,要么引发错误。
使用命令curl www.voxnow.de | hexdump -C,我可以看到对于ö 字符,网页确实是UTF-8 编码(即它包含0xc3 0xb6):
20 74 69 74 6c 65 3d 22 48 69 65 72 20 6b c3 b6 | title="Hier k..|
6e 6e 65 6e 20 53 69 65 20 73 69 63 68 20 6b 6f |nnen Sie sich ko|
73 74 65 6e 6c 6f 73 20 72 65 67 69 73 74 72 69 |stenlos registri|
我已经超出了我的 Python 能力的极限,所以我不知道如何进一步调试它。有什么建议吗?
【问题讨论】:
-
奇怪.. 因为
\u0102\u015b是'Ăś'.. -
@justhalf 我想我看到了这个问题,但不认为我得到了相同的结果。不过我会再检查一次,谢谢。
-
我会倾向于那个答案并使用请求库和原始内容stackoverflow.com/a/36833440/3806595
标签: python unicode utf-8 beautifulsoup urllib2