亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WebScraping & python:在 html 中渲染 javascript?

WebScraping & python:在 html 中渲染 javascript?

繁星coding 2021-09-11 16:27:08
我正在嘗試構建一個網絡爬蟲以從 tsx 頁面獲取趨勢股票。我目前獲得了所有趨勢鏈接,現在我正在嘗試抓取各個頁面上的信息。根據我的代碼,當我嘗試在 getStockDetails() 中輸出“quote_wrapper”時,它返回一個空列表。我懷疑是因為 JavaScript 還沒有在頁面上呈現?不確定這是不是一回事。無論如何,我試圖輸出頁面上的所有html進行調試,我也沒有看到。我讀到“渲染”JavaScript 的唯一方法是使用 Selenium 并使用 browser.execute_script("return document.documentElement.outerHTML")。它適用于索引頁面,所以我嘗試在其他頁面上使用它。我也在代碼中對此進行了評論。謝謝你的幫助,如果可以的話。from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom bs4 import BeautifulSoup as soupfrom urllib2 import urlopen as uReqimport timeimport randomimport requestsdef getTrendingQuotes(source_code):    # grabs all the trending quotes for that day    links = []    page_soup = soup(source_code, "lxml")    trendingQuotes = page_soup.findAll("div", {"id": "trendingQuotes"})    all_trendingQuotes = trendingQuotes[0].findAll('a')    for link in all_trendingQuotes:        url = link.get('href')        name = link.text        # print(name)        links.append(url)    return linksdef getStockDetails(url, browser):    print(url)    source_code = browser.execute_script(        "return document.documentElement.outerHTML")    #What is the correct syntax here?    #I'm trying to get the innerHTML of whole page in selenium driver    #It seems I can only access the JavaScript for the entire page this way    # source_code = browser.execute_script(    #    "return" + url +".documentElement.outerHTML")    page_soup = soup(source_code, "html.parser")    # print(page_soup)    quote_wrapper = page_soup.findAll("div", {"class": "quoteWrapper"})    print(quote_wrapper)def trendingBot(browser):    while True:        source_code = browser.execute_script(            "return document.documentElement.outerHTML")        trending = getTrendingQuotes(source_code)        for trend in trending:            browser.get(trend)            getStockDetails(trend, browser)        break        # print(trend)
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號