我从一些带有Unicode字符的网页中提取HTML,如下所示:
def extract(url):
""" Adapted from Python3_Google_Search.py """
user_agent = ("Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) "
"AppleWebKit/525.13 (KHTML, like Gecko)"
"Chrome/0.2.149.29 Safari/525.13")
request = urllib.request.Request(url)
request.add_header("User-Agent",user_agent)
response = urllib.request.urlopen(request)
html = response.read().decode("utf8")
return html
如你所见,我正在正确解码。所以html
现在是unicode字符串。当打印html时,我可以看到Unicode字符。在
我正在使用html.parser
解析HTML并将其子类化:
当使用类的handle_data
解析出HTML时,Unicode字符似乎被删除/突然消失。文档中没有提到任何关于编码的内容。为什么HTML解析器会删除非ascii字符,我如何解决这样的问题?在
显然,
html.parser
将在遇到非ascii字符时调用handle_entityref
。它传递命名字符引用,为了将其转换为unicode字符,我使用了:Python的文档没有提到这一点。我从未见过比Python更糟糕的文档。在
相关问题 更多 >
编程相关推荐