【发布时间】:2018-09-23 23:34:18
【问题描述】:
我正在尝试使用 BeautifulSoup 解析一些 Facebook 页面以获取验证语句“Verified PageFacebook 确认这是此公众人物、媒体公司或品牌的真实页面。”数据工具提示位置="正确" 这表明该页面已通过验证。我分别用来自直接请求和 Selenium Web 驱动程序的源代码提供 Beautiful Soup,但只能在以后成功:
我首先尝试了请求中的源代码:
import requests
response = requests.get(url)
content = response.content
然后我使用 BeautifulSoup 解析内容并搜索句子。我尝试了不同的解析器,如 html.parser、xml、lxml、html5lib,但没有找到:
soup = BeautifulSoup(content, 'html.parser')
elements = soup.find_all(
attrs={
"data-tooltip-content":
"Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand."
})
elements
[]
所以我尝试使用 Selenium Web Driver 获取 html 源代码并将其提供给 BeautifulSoup,这次我可以成功获取文本:
from selenium import webdriver
soup = BeautifulSoup(text, 'html.parser') # also used xml, lxml, etc, same results
elements = soup.find_all(
attrs={
"data-tooltip-content":
"Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand."
})
我得到了以下,这正是我所需要的。
[<span class="_56_f _5dzy _5d-1 _3twv _33v-" data-hover="tooltip" data-tooltip-content="Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." data-tooltip-position="right" id="u_0_bd"></span>,
<span class="_56_f _5dzy _5dzz _3twv" data-hover="tooltip" data-tooltip-content="Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." data-tooltip-position="right" id="u_0_b0"></span>,
<span class="_56_f _5dzy _5dzz _3twv" data-hover="tooltip" data-tooltip-content="Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." data-tooltip-position="right" id="u_0_b1"></span>,
<span class="_56_f _5dzy _5dzz _3twv" data-hover="tooltip" data-tooltip-content="Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." data-tooltip-position="right" id="u_0_b2"></span>,
<span class="_56_f _5dzy _5dzz _3twv" data-hover="tooltip" data-tooltip-content="Verified PageFacebook confirmed this is an authentic Page for this public figure, media company or brand." data-tooltip-position="right" id="u_0_b4"></span>]
为了了解更多上下文,我使用正则表达式精确定位 _56_f _5dzy _5dzz _3twv 并打印附近的文本以查看我需要的句子是否在源代码中。
import re
re.findall(r".{100}_56_f _5dzy _5d-1 _3twv _33v-.{100}", response.text)
得到以下,没有目标句:
['u003Cspan class=\\"_3d2h\\">\\u003Cspan data-hover=\\"tooltip\\" data-tooltip-position=\\"right\\" class=\\"_56_f _5dzy _5d-1 _3twv _33v-\\" id=\\"u_0_bb\\">\\u003C\\/span>\\u003C\\/span>\\u003C\\/div>"},2],["__markup_072b8e64_0_0",{"__html":"\\u0']
使用 html(由 selenium 网络驱动程序返回):
re.findall(r".{100}_56_f _5dzy _5d-1 _3twv _33v-.{100}", html)
得到:
['1></span></span><span class="_3d2h"><span data-hover="tooltip" data-tooltip-position="right" class="_56_f _5dzy _5d-1 _3twv _33v-" id="u_0_bd" data-tooltip-content=
"Verified PageFacebook confirmed this is an authentic Page for th*',
'3Cspan class=\\"_3d2h\\">\\u003Cspan data-hover=\\"tooltip\\" data-tooltip-position=\\"right\\" class=\\"_56_f _5dzy _5d-1 _3twv _33v-\\" id=\\"u_0_bd\\">\\u003C\\/span>\\u003C\\/span>\\u003C\\/div>"},2],["__markup_072b8e64_0_0",{"']
我不熟悉前端工具,想知道导致不同结果的原因是什么,以及是否有办法让我在不使用 Selenium 网络驱动器的情况下获得完整的 BeautifulSoup 解析树?谢谢!
【问题讨论】:
-
还尝试将请求中的
User-Agent更改为stackoverflow.com/questions/33755849/… 中的stackoverflow.com/questions/33755849/…,但仍然无法正常工作
标签: python html selenium html-parsing