【发布时间】:2013-02-24 11:27:25
【问题描述】:
我正在尝试最终解决一些因尝试使用 lxml 抓取 HTML 而弹出的编码问题。以下是我遇到的三个示例 HTML 文档:
1.
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Unicode Chars: 은 —’</title>
<meta charset='utf-8'>
</head>
<body></body>
</html>
2.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko-KR" lang="ko-KR">
<head>
<title>Unicode Chars: 은 —’</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body></body>
</html>
3.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unicode Chars: 은 —’</title>
</head>
<body></body>
</html>
我的基本脚本:
from lxml.html import fromstring
...
doc = fromstring(raw_html)
title = doc.xpath('//title/text()')[0]
print title
结果是:
Unicode Chars: ì ââ
Unicode Chars: 은 —’
Unicode Chars: 은 —’
所以,样品 1 和缺少的 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 标记显然存在问题。 here 的解决方案将正确地将示例 1 识别为 utf-8,因此它在功能上等同于我的原始代码。
lxml 文档出现冲突:
来自here 的示例似乎建议我们应该使用 UnicodeDammit 将标记编码为 unicode。
from BeautifulSoup import UnicodeDammit
def decode_html(html_string):
converted = UnicodeDammit(html_string, isHTML=True)
if not converted.unicode:
raise UnicodeDecodeError(
"Failed to detect encoding, tried [%s]",
', '.join(converted.triedEncodings))
# print converted.originalEncoding
return converted.unicode
root = lxml.html.fromstring(decode_html(tag_soup))
但是 here 它说:
[Y]当您尝试 [解析] Unicode 字符串中的 HTML 数据(该字符串在标头的元标记中指定字符集)时,您将收到错误消息。在将 XML/HTML 数据传递给解析器之前,您通常应该避免将其转换为 unicode。它既慢又容易出错。
如果我尝试遵循 lxml 文档中的第一个建议,我的代码现在是:
from lxml.html import fromstring
from bs4 import UnicodeDammit
...
dammit = UnicodeDammit(raw_html)
doc = fromstring(dammit.unicode_markup)
title = doc.xpath('//title/text()')[0]
print title
我现在得到以下结果:
Unicode Chars: 은 —’
Unicode Chars: 은 —’
ValueError: Unicode strings with encoding declaration are not supported.
示例 1 现在可以正常工作,但示例 3 由于 <?xml version="1.0" encoding="utf-8"?> 标记而导致错误。
是否有正确的方法来处理所有这些情况?有没有比以下更好的解决方案?
dammit = UnicodeDammit(raw_html)
try:
doc = fromstring(dammit.unicode_markup)
except ValueError:
doc = fromstring(raw_html)
【问题讨论】:
标签: python unicode web-scraping beautifulsoup lxml