zoukankan      html  css  js  c++  java
  • Selenium+PhantomJS替代方案

    问题描述:

    python3在使用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

    翻译过来就是:selenium已经放弃PhantomJS了,建议使用火狐或者谷歌无界面浏览器。

    so,PhantomJS要退出历史舞台了。

    解决方案:

    selenium版本降级(不推荐)

    通过pip show selenium显示,默认安装版本为3.8.1。

    将其卸载pip uninstall selenium,重新安装并指定版本号pip install selenium==2.48.0。

    再次运行,发现没有报错。

    使用无界面浏览器

    Selenium+Headless Firefox

    Selenium+Headless Firefox和Selenium+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.baidu.com/")
        print(driver.page_source)
        driver.close()
    
    if __name__ == '__main__':
        main()

    Selenium+Headless Chrome

    前提条件:

    - 本地安装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()

    python selenium模块使用出错,找不到路径

    selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

    Windows环境下:

    1、下载geckodriver.exe:

    地址:https://github.com/mozilla/geckodriver/releases

    请根据系统版本选择下载;(如Windows 64位系统)

    2、下载解压后将getckodriver.exe复制到Firefox的安装目录下,如(C:Program FilesMozilla Firefox),并在环境变量Path中添加路径:C:Program FilesMozilla Firefox;

    3.重启cmd或IDLE再次运行代码即可

    ubuntu16.04环境下

    1、下载 geckodriverckod

    地址: https://github.com/mozilla/geckodriver/releases

    2、解压后将geckodriverckod 存放至 /usr/local/bin/ 路径下即可

    参考链接:

    https://blog.csdn.net/liu5257/article/details/53437878

    https://blog.csdn.net/u010358168/article/details/79749149

  • 相关阅读:
    python基础之数值类型
    第一个python学习笔记
    入学潭州科技教育学习python语言
    Python 编码规范(Google)
    Python
    博客开通
    文件、异常、文件的输入输出
    内置函数,变量的作用域,闭包及递归
    序列类型各自的方法(1)
    函数基础
  • 原文地址:https://www.cnblogs.com/zhaijiahui/p/10622770.html
Copyright © 2011-2022 走看看