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 '

翻译过来就是:

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

解决方案

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()
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()

如上,完美解决~

Selenium+PhantomJS使用时报错原因及解决方案(转)的更多相关文章

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

    问题 今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium support for PhantomJS has been dep ...

  2. Selenium+PhantomJS使用时报错原因

    运行下面代码:'''PhantomJS运用''' from selenium import webdriverimport time # 通过keys模拟键盘from selenium.webdriv ...

  3. 分析https网页加载http资源导致的页面报错原因及其解决方案

    https网页加载http资源导致的页面报错及解决方案 https是当下的网站的主流趋势,甚至像苹果这样的大公司,则完全要求用户必须使用https地址. 然而对于以前http链接来说,我们往往就存在一 ...

  4. angularJS中$http.get( ).success( )报错原因及解决方案

    一.问题描述: 电脑安装的angular1.6.7版本,项目中使用了$http.get( ).success( ),控制台报错: $http.get(...).success is not a fun ...

  5. ie7,8下__flash__addCallback报错原因及解决方案

    首先附张图,以直观说明错误的现象:(ie7.8)

  6. C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    有人在社区问到:C#调用Oracle中自定义函数的返回值时,无法正常调用.但在PL/SQL中正常调用返回. 于是动手一试: 1.准备函数(Oralce 11g.2.0.0.4) CREATE OR R ...

  7. Selenium chromeDriver启动时报错:session not created: This version of ChromeDriver only supports Chrome

    解决方案: 这是因为ChromeDriver与本地chrome浏览器的版本不一致导致 ChromeDriver下载地址:http://npm.taobao.org/mirrors/chromedriv ...

  8. python中用selenium调Firefox报错问题

    python在用selenium调Firefox时报错: Traceback (most recent call last):  File "G:\python_work\chapter11 ...

  9. nginx启动时报错

    nginx启动时报错 原因:nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed /var/cache/ngin ...

随机推荐

  1. eclipse 安装教程

    eclipse 安装教程 一:安装包下载: 链接: https://pan.baidu.com/s/1qZtt62o 密码: 4ak2 注:若 下载链接失效,请看本文公告的QQ群,请联系群主. 二:安 ...

  2. Confluence 6 PostgreSQL 测试你的数据库连接

    在你的数据库设置界面,有一个 测试连接(Test connection)按钮可以检查: Confluence 可以连接你的数据库服务器 数据库字符集的编码是否设置正确 你的数据库用户是否有正确的权限可 ...

  3. UserNotifications ios10 通知使用

    通知在ios10 中推荐使用 导入  import UserNotifications  头文件 if #available(iOS 10.0, *) { UNUserNotificationCent ...

  4. nginx之访问控制http_access_module与http_auth_basic_module

    http_access_module 作用 基于IP的访问控制 语法 使用 局限性 解决办法 1. http_x_forwarded_for http_auth_basic_module 作用 基于用 ...

  5. LeetCode(107): 二叉树的层次遍历 II

    Easy! 题目描述: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], ...

  6. Python知乎热门话题数据的爬取实战

    import requestsfrom pyquery import PyQuery as pq url = 'https://www.zhihu.com/explore'headers = { 'u ...

  7. Python(列表操作应用实战)方法二

    # 输入一个数据,删除一个列表中的所有指定元素# 给定的列表数据data = [1,2,3,4,5,6,7,8,9,0,5,4,3,5,"b","a",&quo ...

  8. 插件使用一树形插件---zTree一zTree异步加载

    zTree 可以实现异步加载.异步加载可以让初次加载速度快,带来好的用户体验. 异步加载 官方源码中的demo提示了例子.例子是采用php语言. 在java语言中,zTree如何与Servlet结合呢 ...

  9. k-近邻算法-优化约会网站的配对效果

    KNN原理 1. 假设有一个带有标签的样本数据集(训练样本集),其中包含每条数据与所属分类的对应关系. 2. 输入没有标签的新数据后,将新数据的每个特征与样本集中数据对应的特征进行比较. a. 计算新 ...

  10. 第二种掌握的排序Q-Q

    #include<stdio.h> int main() {     int s[10000]={0};     int i=0,j=0,n=0,x=0;     scanf(" ...