HTML编码和lxml解析
我试图最终解决一些编码问题,这些问题从尝试使用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"
/>
标签的问题。这里的解决方案将正确地将示例1识别为utf-8,因此在功能上与我的原始代码等效。
lxml文档出现冲突:
从这里开始,该示例似乎建议我们应该使用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))
但是在这里它说:
当您尝试[解析] Unicode字符串中的HTML数据时会出错,该字符串在标头的meta标签中指定了字符集。通常应避免在将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可以正常工作,但由于<?xml version="1.0" encoding="utf-8"?>
标签,示例3导致错误。
是否有处理所有这些情况的正确方法?是否有比以下更好的解决方案?
dammit = UnicodeDammit(raw_html)
try:
doc = fromstring(dammit.unicode_markup)
except ValueError:
doc = fromstring(raw_html)
-
lxml
有一些
与处理Unicode有关的问题。最好在明确指定字符编码时使用字节(目前):#!/usr/bin/env python import glob from lxml import html from bs4 import UnicodeDammit for filename in glob.glob('*.html'): with open(filename, 'rb') as file: content = file.read() doc = UnicodeDammit(content, is_html=True) parser = html.HTMLParser(encoding=doc.original_encoding) root = html.document_fromstring(content, parser=parser) title = root.find('.//title').text_content() print(title)
输出量
Unicode Chars: 은 —’ Unicode Chars: 은 —’ Unicode Chars: 은 —’