震惊!Selenium分手PhantomJS

背景

今天本地调试基于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('http://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

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

碎碎念

上面提到的那首歌中的反対此处是相反的意思,不是汉语的反对,但是,真的好来感~

第一段中提到的今天现在已经成昨天了,又到情人节了哎,好像去年情人节的时候也写了一篇博客吧,去年也是像现在一样单身吧……怎么说着说着眼泪就留下来了呢……

  • 23
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 24
    评论
需要的添加的jar包及工具:我这里使用maven来构建项目,添加依赖如下:   org.seleniumhq.selenium   selenium-java   3.2.0    PhantomJs工具到官网去下载:http://phantomjs.org/download.html 尽量都使用最新版本,不然会出现版本兼容的情况。 这里有一个已经写好的获取PhantomJSDriver的工具类 public static WebDriver getPhantomJs() {   String osname = System.getProperties().getProperty("os.name");   if (osname.equals("Linux")) {//判断系统的环境win or Linux     System.setProperty("phantomjs.binary.path", "/usr/bin/phantomjs");   } else {     System.setProperty("phantomjs.binary.path", "./phantomjs/win/phantomjs.exe");//设置PhantomJs访问路径   }   DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();   //设置参数   desiredCapabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");   desiredCapabilities.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101   Firefox/50.0");   if (Constant.isProxy) {//是否使用代理     org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();     proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);     proxy.setAutodetect(false);     String proxyStr = "";     do {       proxyStr = ProxyUtil.getProxy();//自定义函数,返回代理ip及端口     } while (proxyStr.length() == 0);     proxy.setHttpProxy(proxyStr);     desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);   }   return new PhantomJSDriver(desiredCapabilities); } 获取方式     try{     WebDriver webDriver = PhantomJsUtil.getPhantomJs();     webDriver.get(url);     SleepUtil.sleep(Constant.SEC_5);     PhantomJsUtil.screenshot(webDriver);     WebDriverWait wait = new WebDriverWait(webDriver, 10);     wait.until(ExpectedConditions.presenceOfElementLocated(By.id(inputId)));//开始打开网页,等待输入元素出现     Document document = Jsoup.parse(webDriver.getPageSource());     //TODO  剩下页面的获取就按照Jsoup获取方式来做   }finally{     if (webDriver != null) {       webDriver.quit();     }   } python版使用webdriver+PhantomJs爬虫使用,参考http://www.cnblogs.com/kuqs/p/6395284.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔天逸

没有钱用,只能写写博客这样子~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值