背景

今天本地调试基于Selenium+PhantomJS的动态爬虫程序顺利结束后,着手部署到服务器上,刚买的热乎的京东云,噼里啪啦一顿安装环境,最后跑的时候报了这么个错误:

UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead

运用我考了五遍才飘过的六级英语定睛一看,这个意思是说,新版本的Selenium不再支持PhantomJS了,请使用Chrome或Firefox的无头版本来替代。脑瓜里瞬间响起了这首歌的旋律,简直不能接受,凭什么就把我们PhantomJS抛弃了(╯‵□′)╯︵┻━┻。

因为这半年都没有写爬虫的需求,并且最近一直在本地用的老版本Selenium开发,所以一直还PhantomJS的很嗨,殊不知脚步已经落后了。查了一下,大概是去年七八月份Chrome和Firefox相继推出了无头浏览器模式。可能就因为这样,PhantomJS独领风骚的局面瞬间丧失,然后逐渐消失在历史的尘埃中吧……小厂出的创新产品,大厂做出类似产品之后,小厂GG,大概也是这么一回事吧……

Selenium+Headless Firefox

虽然很不情愿,但是人不能总是追忆过去的美好,该往前走的时候就要往前走对吧~

其实Selenium+Headless Firefox没什么好说的,跟Selenium+Friefox的区别就是实例化的时候传个参数而已。

需要注意的点就是:

本地要有Firefox,不然报找不到载体 本地要有geckodriver,最好再配置一下环境变量 没了

各个语言的示例代码都可以在这个链接里找到,这里就搬运一下python的示例代码吧:

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as expected
from selenium.webdriver.support.wait import WebDriverWait if __name__ == "__main__":
options = Options()
options.add_argument('-headless') # 无头参数
driver = Firefox(executable_path='geckodriver', firefox_options=options) # 配了环境变量第一个参数就可以省了,不然传绝对路径
wait = WebDriverWait(driver, timeout=10)
driver.get('https://www.google.com')
wait.until(expected.visibility_of_element_located((By.NAME, 'q'))).send_keys('headless firefox' + Keys.ENTER)
wait.until(expected.visibility_of_element_located((By.CSS_SELECTOR, '#ires a'))).click()
print(driver.page_source)
driver.quit()

win10和ubuntu下我都测试了没问题,从前实例化一个PhantomJS大约3秒,Headless Firefox的话,7秒左右……其实无伤大雅。

这里友情提示一下新手小伙伴,别每下载一个网页实例化一个webdriver(Firefox or Chrome)然后就close()掉,实例化webdriver的时间也是时间~推荐将下载器做成单例类或将webdirver做类变量。

Selenium+Headless Chrome

这个跟上边大同小异,我没试,传送门:

PhantomJS在Selenium中被标记为过时的应对措施 Getting Started with Headless Chrome

Selenium+Headless Firefox的更多相关文章

  1. 爬虫(三)通过Selenium + Headless Chrome爬取动态网页

    一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...

  2. selenium启动firefox时加载扩展

    有些时候,我们测试需要用到插件或者已经导入的证书(比如金融和安全加密行业),而selenium启动firefox时会打开一个新的,不含有任何插件和个人证书的firefox(等同于全新安装后第一次打开的 ...

  3. selenium和Firefox版本不兼容

    selenium8.py coding = utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.get(' ...

  4. selenium - Headless Browser and scraping - solutions - Stack Overflow

    yum install pygtk2-devel   selenium - Headless Browser and scraping - solutions - Stack Overflow Hea ...

  5. 解决selenium驱动Firefox跳转页慢慢慢的问题

    首先我给自己定义为是一个更新偏执狂.不知道从哪个版本开始,使用selenium驱动打开Firefox浏览器时,会跳转到官网指定页,这个过程真是慢得要死. 为了解决这个问题,我是查了很多资料,解决方案是 ...

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

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

  7. selenium启动Firefox失败

    今天搭建java+selenium环境,搭建几次都失败,总结一下原因 1. selenium启动Firefox,不需要额外的driver 2. Friefox如果没有安装到默认路径C盘,代码中需要修改 ...

  8. Selenium和firefox兼容性问题

    Selenium和firefox兼容性问题 2016-07-10 若出现兼容性问题,会报如下错误: org.openqa.selenium.firefox.NotConnectedException: ...

  9. Selenium在Firefox中踩过的

    本文转至 http://www.51testing.com/html/11/n-3711311.html,作者对webdriver在Firefox中设置profile配置项挺熟的,是用Python实现 ...

随机推荐

  1. jode反编译软件

    1.下载 http://jode.sourceforge.net/(官网) https://sourceforge.net/projects/jode/files/(下载地址) 2.使用 下载的jod ...

  2. python 调用阿里云云解析api添加记录

    首先安装阿里云SDK pip install aliyun-python-sdk-core pip install aliyun-python-sdk-alidns 可以配合jenkins传递参数 # ...

  3. 网站被植入Webshell的解决方案

    什么是Webshell 从字面上理解,”Web”指需要服务器开放Web服务,”shell”指取得对服务器的某种程度的操作权限.Webshell指匿名用户(入侵者)通过网站端口,获取网站服务器的一定操作 ...

  4. Python的Matplotlib库简述

    Matplotlib 库是 python 的数据可视化库import matplotlib.pyplot as plt 1.字符串转化为日期 unrate = pd.read_csv("un ...

  5. 如何使用Hive&R从Hadoop集群中提取数据进行分析

    一个简单的例子! 环境:CentOS6.5 Hadoop集群.Hive.R.RHive,具体安装及调试方法见博客内文档. 1.分析题目 --有一个用户数据样本(表名huserinfo)10万数据左右: ...

  6. 常用 shell 命令

    watch watch -n 1 -d "ps -ef | grep php" 功能:监控 PHP 进程的变化 原理: 重复的执行后面的命令,默认时间间隔是2秒 参数解释 -d 高 ...

  7. cocos2d JS 设置字幕循环滚动(背景图滚动亦可)

    var dong = ccs.load("res/Login.json"); this.addChild(dong.node); this.cShamNotice = ccui.h ...

  8. 第四章 CSS3概述

    1.CSS3新增常用选择器(1)结构性伪类选择器:root 文档根元素 :nth-child(n) 第N个子元素"first-child 第一个元素 :kast-child 最后一个子元素 ...

  9. mybatis oracle -批量插入,存在则更新

    <insert id="batchUpdatePBWUserInfo" parameterType="java.util.List"> MERGE ...

  10. 取n到m行

    取n到m行 . select top m * from tablename where id not in (select top n id from tablename order by id as ...