【问题标题】:Scraping a JavaScript rendered page抓取 JavaScript 呈现的页面
【发布时间】:2018-06-12 02:25:57
【问题描述】:

我想在 Python3 中使用 Selenium Web 驱动程序从 Javascript 呈现的页面中提取一些数据。我尝试了几个驱动程序,例如 Firefox、Chromedriver 和 PhantomJS,但总是得到相同的结果。我只得到了脚本,而不是 DOM 元素。

这是我的代码的 sn-p

url = 'https://www.google.com/flights/explore/#explore;f=BDO;t=r-Asia-0x88d9b427c383bc81%253A0xb947211a2643e5ac;li=0;lx=2;d=2018-01-09'
driver = webdriver.Chrome("/var/chromedriver/chromedriver")
driver.implicitly_wait(20)
driver.get(url)

print(driver.page_source)

我错过了什么吗?

【问题讨论】:

  • 您有错误信息吗?在帖子中推送您的回溯消息。
  • 执行这些代码时没有错误消息。它只是给了我一个意想不到的结果

标签: javascript python-3.x selenium selenium-webdriver


【解决方案1】:

我在您的代码块中没有看到任何此类问题。我试过你自己的脚本如下:

from selenium import webdriver

url = 'https://www.google.com/flights/explore/#explore;f=BDO;t=r-Asia-0x88d9b427c383bc81%253A0xb947211a2643e5ac;li=0;lx=2;d=2018-01-09'
driver = webdriver.Chrome()
driver.get(url)
print(driver.page_source)

我得到以下控制台输出:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <meta name="deals::gwt:property" content="baseUrl=/flights/explore//static/" />
  <title>Explore flights</title>
  <meta name="description" content="Explore flights" />
  <script src="https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.yoTdpQipo6s.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/am=AAE/rs=AHpOoo9_VhuRoUovwpPPf5LqLZd-dmCnxw/cb=gapi.loaded_0" async=""></script>
  <script language="javascript" type="text/javascript">
    var __JS_ILT__ = new Date();
    .
    .
    . <
    /div></div > < div aria - hidden = "true"
    style = "display: none;" > < div class = "CTPFVNB-l-j CTPFVNB-l-h" > Displayed currencies may differ from the currencies used to purchase flights.– < a href = "https://www.google.com/intl/en/googlefinance/disclaimer/"
    class = "CTPFVNB-l-k" > Disclaimer < /a></div > < /div><div aria-hidden="true" style="display: none;"><div class="CTPFVNB-l-j CTPFVNB-l-h">Showing licensed rail data. – <a href="https:/ / www.google.com / intl / en / help / legalnotices_maps.html " class="
    CTPFVNB - l - k ">Legal Notice</a></div></div><div class="
    CTPFVNB - l - i "><a class="
    CTPFVNB - l - k CTPFVNB - l - j " href="
    https: //www.google.com/intl/en/policies/">Privacy &amp; Terms</a><a class="CTPFVNB-l-k CTPFVNB-l-j" href="https://support.google.com/flights/?hl=en">Help Center</a></div></div></div><iframe id="deals" tabindex="-1" style="position: absolute; width: 0px; height: 0px; border: none; left: -1000px; top: -1000px;">
</iframe><input type="text" id="_bgInput" style="display:none;" /></body></html>

现在,您可以清楚地看到 page_source 的 fag 末尾有一个 iframe。因此,除非我们切换到 iframe,否则您将无法找到您正在寻找的 DOM element

【讨论】:

  • 感谢您的解释。但是,问题是 page_source 的输出与我在检查页面时得到的不同。例如,我想获取所有可用的价格。当我尝试从 page_source 解析它时,它不会返回任何内容,因为其中不包含价格。如果我在检查的元素中看到,价格存在于 iframe 标记之外。
  • 是的,你是对的。切换到iframe 并获取page_source,您将找到所有内容。我没有观察到问题中提到的任何price。随意根据您的新要求提出新问题。如果我的Answer 满足您的QuestionAccept Answer
【解决方案2】:

使用氦作为硒包装

# pip install helium
import helium, time
url_one = "https://www.vbiz.in/nseoptionchain.html"
browser_one = helium.start_chrome(url_one, headless=True)
seconds = 5
time.sleep(seconds)
html = browser_one.page_source
browser_one.close()

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 2018-08-09
    • 2019-06-13
    • 2019-10-05
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多