【发布时间】:2019-10-11 03:48:56
【问题描述】:
我使用 lxml.html 来解析各种 html 页面。现在我认识到,至少对于某些页面,尽管它存在并且漂亮的汤找到了它(即使它使用 lxml 作为解析器),但它没有找到 body 标签。
示例页面: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')
【问题讨论】:
-
你的
html_string是什么? -
@JackFleeting 它的plus.google.com 的内容。我没有添加它,因为它很大。
标签: python beautifulsoup lxml