python - BeautifulSoup 给了我 unicode+html 符号,而不是直接的 unicode。这是错误还是误解?

标签 python html unicode beautifulsoup

我正在使用 BeautifulSoup 抓取网站。该网站的页面在我的浏览器中呈现良好:

Oxfam International’s report entitled “Offside! http://www.coopamerica.org/programs/responsibleshopper/company.cfm?id=271

特别是,单引号和双引号看起来没问题。它们看起来是 html 符号而不是 ascii,尽管奇怪的是当我在 FF3 中查看源代码时它们看起来是正常的 ascii。

不幸的是,当我抓取时,我得到了这样的东西

u'Oxfam International\xe2€™s report entitled \xe2€œOffside!

哎呀,我的意思是:

u'Oxfam International\xe2€™s report entitled \xe2€œOffside!

页面的元数据表示“iso-88959-1”编码。我尝试了不同的编码,玩过 unicode->ascii 和 html->ascii 第三方函数,并查看了 MS/iso-8859-1 差异,但事实是 ™ 与单引号,而且我似乎无法将 unicode+htmlsymbol 组合转换为正确的 ascii 或 html 符号——以我有限的知识,这就是我寻求帮助的原因。

我很乐意使用 ascii 双引号,"或 "

下面的问题是我担心还有其他有趣的符号解码不正确。

\xe2€™

下面是一些 python 来重现我所看到的,然后是我尝试过的东西。

import twill
from twill import get_browser
from twill.commands import go

from BeautifulSoup import BeautifulSoup as BSoup

url = 'http://www.coopamerica.org/programs/responsibleshopper/company.cfm?id=271'
twill.commands.go(url)
soup = BSoup(twill.commands.get_browser().get_html())
ps = soup.body("p")
p = ps[52]

>>> p         
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe2' in position 22: ordinal not in range(128)

>>> p.string
u'Oxfam International\xe2€™s report entitled \xe2€œOffside!<elided>\r\n'

http://groups.google.com/group/comp.lang.python/browse_frm/thread/9b7bb3f621b4b8e4/3b00a890cf3a5e46?q=htmlentitydefs&rnum=3&hl=en#3b00a890cf3a5e46

http://www.fourmilab.ch/webtools/demoroniser/

http://www.crummy.com/software/BeautifulSoup/documentation.html

http://www.cs.tut.fi/~jkorpela/www/windows-chars.html

>>> AsciiDammit.asciiDammit(p.decode())
u'<p>Oxfam International\xe2€™s report entitled \xe2€œOffside!

>>> handle_html_entities(p.decode())
u'<p>Oxfam International\xe2\u20ac\u2122s report entitled \xe2\u20ac\u0153Offside! 

>>> unicodedata.normalize('NFKC', p.decode()).encode('ascii','ignore')
'<p>Oxfam International€™s report entitled €œOffside!

>>> htmlStripEscapes(p.string)
u'Oxfam International\xe2TMs report entitled \xe2Offside!

编辑:

我试过使用不同的 BS 解析器:

import html5lib
bsoup_parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("beautifulsoup"))
soup = bsoup_parser.parse(twill.commands.get_browser().get_html())
ps = soup.body("p")
ps[55].decode()

这给了我这个

u'<p>Oxfam International\xe2\u20ac\u2122s report entitled \xe2\u20ac\u0153Offside!

最好的情况解码似乎给我相同的结果:

unicodedata.normalize('NFKC', p.decode()).encode('ascii','ignore')
'<p>Oxfam InternationalTMs report entitled Offside! 

编辑 2:

我正在运行带有 FF 3.0.7 和 Firebug 的 Mac OS X 4

Python 2.5(哇,不敢相信我从一开始就没有说明这一点)

最佳答案

这是一个严重困惑的页面,编码方面:-)

您的方法完全没有错。我可能会倾向于在将它传递给 BeautifulSoup 之前进行转换,只是因为我很顽固:

import urllib
html = urllib.urlopen('http://www.coopamerica.org/programs/responsibleshopper/company.cfm?id=271').read()
h = html.decode('iso-8859-1')
soup = BeautifulSoup(h)

在这种情况下,页面的元标记与编码有关。该页面实际上是 utf-8...Firefox 的页面信息揭示了真正的编码,您实际上可以在服务器返回的响应 header 中看到这个字符集:

curl -i http://www.coopamerica.org/programs/responsibleshopper/company.cfm?id=271
HTTP/1.1 200 OK
Connection: close
Date: Tue, 10 Mar 2009 13:14:29 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Set-Cookie: COMPANYID=271;path=/
Content-Language: en-US
Content-Type: text/html; charset=UTF-8

如果您使用“utf-8”进行解码,它将对您有用(或者至少对我有用):

import urllib
html = urllib.urlopen('http://www.coopamerica.org/programs/responsibleshopper/company.cfm?id=271').read()
h = html.decode('utf-8')
soup = BeautifulSoup(h)
ps = soup.body("p")
p = ps[52]
print p

关于python - BeautifulSoup 给了我 unicode+html 符号,而不是直接的 unicode。这是错误还是误解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/629999/

相关文章:

c - glib gunichar 和 wchar_t 之间有什么区别,哪个更适合跨平台解决方案?

java - 中文字符在 Mac 终端中显示为问号

python jira 无法转换问题

python - 缺少 userloader 或 request_loader 错误

html - iPad、Safari 浏览器出现异常边距

html - 两列 html 布局,带拉伸(stretch)以适合列

用于特殊 unicode 字符的 iOS5 精美图标(表情符号?)-不是我想要的

python - Django:扩展覆盖的模板

python - print() 有时无法在命令提示符中显示

CSS 背景附件不起作用,无法修复背景上的填充/边距