Python3利用Chrome的--headless爬取動態頁面內容(推薦)


Python3利用Chrome的--headless爬取動態頁面內容(推薦)

利用Chrome瀏覽器的--headless爬取頁面的動態內容:

1,下載與瀏覽器匹配的chromdriver.exe,並將文件拷貝到python的Scripts的目錄下(已配置環境變量path的);

2,示例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def getevfromtt(code):
    try:
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')
        driver = webdriver.Chrome(chrome_options=chrome_options)
        driver.get("http://fund.eastmoney.com/" + code + ".html")
        driver.execute_script("window.scrollTo(0,document.body.scrollHeight);") #將頁面拉到底部
        ljkq_btn = driver.find_elements_by_class_name("ip_tips_btn")[0]
        if ljkq_btn is not None:
            ljkq_btn.find_element_by_tag_name("span").click()
        time.sleep(0.5)
        ev = driver.find_element_by_id("gz_gsz").text
        ev_change = driver.find_element_by_id("gz_gszzl").text
        if ev=="--":
            ev = 0.0000
        driver.quit()
        logger.info("getevfromtt>>:獲取成功")
        return ev, change_percent(ev_change)
    except BaseException as e:
        logger.info("getevfromtt>>:獲取異常"+e)
        return "", ""    

if __name__ == "__main__":
    getevfromtt('340007');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM