【问题标题】:Selenium Webdrive better than direct request?Selenium Webdriver 比直接请求更好?
【发布时间】: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\\"&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 解析树?谢谢!

【问题讨论】:

标签: python html selenium html-parsing


【解决方案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/

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

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

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

【讨论】:

  • 谢谢。是的,我确实想得到整个句子,所以看起来解释 javascript 形式是不可避免的。
猜你喜欢
  • 1970-01-01
  • 2018-01-07
  • 2018-08-20
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多