Selenium Webdrive比直接请求更好吗?

时间:2018-04-13 14:56:31

标签: python html selenium html-parsing

我正在尝试使用BeautifulSoup解析一些Facebook页面以获得验证句子“已验证的PageFacebook确认这是这个公众人物,媒体公司或品牌的真实页面。”数据提示位置=“右” 这表明页面已经过验证。我使用来自直接请求的源代码和Selenium Web Driver单独提供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 web驱动程序返回):

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\\"&gt;\\u003Cspan data-hover=\\"tooltip\\" data-tooltip-position=\\"right\\" class=\\"_56_f _5dzy _5d-1 _3twv _33v-\\" id=\\"u_0_bd\\"&gt;\\u003C\\/span&gt;\\u003C\\/span&gt;\\u003C\\/div&gt;"},2],["__markup_072b8e64_0_0",{"']

我不熟悉前端工具,想知道不同结果的原因是什么,以及是否有办法让我在不使用Selenium网络驱动器的情况下获得完整的BeautifulSoup解析树?谢谢!

1 个答案:

答案 0 :(得分:1)

这个问题是html是使用javascript生成的。如果您查看html源代码,您可以看到Verified就在那里,它只是在javascript的数据中。

["__markup_d3c2dfe2_0_0",{"__html":"\u003Cdiv class=\"_4ag8\">\u003Cdiv class=\"_50f7\">Verified Page\u003C\/div>\u003Cspan>Facebook confirmed this is an authentic Page for this public figure, media company or brand.\u003C\/span>\u003C\/div>"},1]

视图源:https://www.facebook.com/barackobama/

如果您不想使用硒,我建议您搜索已验证的请求文本。

requests.get(url).text.find('Verified')

它在selenium中工作,因为浏览器执行javascript来构建html。