zoukankan      html  css  js  c++  java
  • Selenium+PhantomJS使用时报错原因及解决方案

    Selenium+PhantomJS使用时报错原因及解决方案

    问题

    今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息:

    UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
      warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
    • 1
    • 2

    翻译过来就是:

    selenium已经放弃PhantomJS,了,建议使用火狐或者谷歌无界面浏览器。
    • 1

    解决方案

    selenium版本降级

    通过pip show selenium显示,默认安装版本为3.8.1。 
    将其卸载pip uninstall selenium,重新安装并指定版本号pip install selenium==2.48.0。 
    再次运行,发现没有报错,搞定!

    使用无界面浏览器

    Selenium+Headless Firefox

    Selenium+Headless FirefoxSelenium+Firefox,区别就是实例option的时候设置-headless参数。

    前提条件: 
    - 本地安装Firefox浏览器 
    - 本地需要geckodriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

    示例代码:

    from selenium.webdriver import Firefox
    from selenium.webdriver.firefox.options import Options
    
    
    def main():
        options = Options()
        options.add_argument('-headless')
        driver = Firefox(executable_path='./geckodriver', firefox_options=options)
        driver.get("https://www.qiushibaike.com/8hr/page/1/")
        print(driver.page_source)
        driver.close()
    
    
    if __name__ == '__main__':
        main()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    Selenium+Headless Chrome

    Firefox类似,双手奉上。

    前提条件: 
    - 本地安装Chrome浏览器 
    - 本地需要chromedriver驱动器文件,如果不配置环境变量的话,需要手动指定executable_path参数。

    示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    
    def main():
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=chrome_options)
        driver.get("https://www.baidu.com")
        print(driver.page_source)
        driver.close()
    
    
    if __name__ == '__main__':
        main()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    如上,完美解决~

  • 相关阅读:
    线索二叉树的构建和遍历------小甲鱼数据结构和算法
    小甲鱼数据结构和算法-----二叉树的构建和前序遍历
    python爬虫爬取煎蛋网妹子图片
    C语言实现汉诺塔问题
    C语言实现中缀表达式转后缀表达式
    深度优先算法--判断迷宫的一个起点能否到达一个终点
    python 爬取36K新闻
    栈的操作实现逆波兰表达式的计算
    python 实现汉诺塔问题
    【POJ 3258】River Hopscotch
  • 原文地址:https://www.cnblogs.com/qiangyuzhou/p/11021719.html
Copyright © 2011-2022 走看看