我使用lxml.html解析各种html页面。现在我意识到,至少在某些页面上,它没有找到body标记,尽管它存在,而漂亮的汤找到了它(尽管它使用lxml作为解析器)。
示例页面:https://plus.google.com/ (剩余内容)
import lxml.html
import bs4
html_string = """
... source code of https://plus.google.com/ (manually copied) ...
"""
# lxml fails (body is None)
body = lxml.html.fromstring(html_string).find('body')
# Beautiful soup using lxml parser succeeds
body = bs4.BeautifulSoup(html_string, 'lxml').find('body')任何关于这里正在发生的事情的猜测都是受欢迎的:)
更新:
这个问题似乎与编码有关。
# working version
body = lxml.html.document_fromstring(html_string.encode('unicode-escape')).find('body')发布于 2019-05-24 14:42:56
你可以使用这样的东西:
import requests
import lxml.html
html_string = requests.get("https://plus.google.com/").content
body = lxml.html.document_fromstring(html_string).find('body')主体变量包含body html元素。
https://stackoverflow.com/questions/56294378
复制相似问题