我有一个Python脚本,我得到一些HTML和解析它使用美丽的汤。在HTML中,有时候没有unicode字符,它会导致我的脚本和我创建的文件出错。Python - HTML到Unicode
这里是我如何获得HTML
html = urllib2.urlopen(url).read().replace(' ',"")
xml = etree.HTML(html)
当我使用这个
html = urllib2.urlopen(url).read().encode('ascii', 'xmlcharrefreplace')
我得到一个错误UnicodeDecodeError
我怎么会变成这样的unicode。所以如果有非Unicode字符,我的代码不会中断。
[将HTML实体转换为Unicode,反之亦然](http:// stackoverflow。com/questions/701704/convert-html-entities-to-unicode-反之亦然) – 2014-11-03 20:58:24
@AlexThornton当我使用它时,我得到一个错误UnicodeDecodErro – iqueqiorio 2014-11-03 21:00:49
你能给一个例子输入和输出字符串的例子,你可能会期待? – 2014-11-03 21:15:45