zoukankan      html  css  js  c++  java
  • 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');
  • 相关阅读:
    写在毕业散伙饭后
    关于2005年高考志愿填报注意事项与分析
    如何在ASP.NET页面间传送数据
    MS Server 2000中的Table类型
    离职申请[转]
    JQUERY 获取text,areatext,radio,checkbox,select值
    DropdownList用法记录
    SQL小记
    认识SortedList类
    js中的|| 与 &&
  • 原文地址:https://www.cnblogs.com/lizm166/p/13877515.html
Copyright © 2011-2022 走看看