需要的添加的jar包及工具:我这里使用maven来构建项目,添加依赖如下:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.2.0</version>
</dependency>  

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

WebDriver+PhantomJs爬虫运用(Java)的更多相关文章

  1. 用webdriver+phantomjs实现无浏览器的自动化过程

    环境准备 1. 安装python: 2. 安装pip: 3. 通过pip安装selenium: 4. 下载phantomJS的包并解压缩: 1. 若在Windows系统中,将下载的phantomjs文 ...

  2. 使用webdriver + phantomjs + pdfkit 生成PDF文件

    实例 #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on Dec 6, 2013 @author: Jay <smile665@gm ...

  3. Python PhantomJS 爬虫 示例

    from selenium import webdriver# 请求url url = "https://auctions.freemansauction.com/auction-lot-d ...

  4. selenium+phantomJS爬虫,适用于登陆限制强,点触验证码等一些场景

    selenium是非常出名的自己主动化測试工具,多数场景是測试project师用来做自己主动化測试,可是相同selenium能够作为基本上模拟浏览器的工具,去爬取一些基于http request不能或 ...

  5. 【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)

    一.selenium2.0简述 与一般的浏览器测试框架(爬虫框架)不同,Selenium2.0实际上由两个部分组成Selenium+webdriver,Selenium负责用户指令的解释(code), ...

  6. 网络爬虫(java)

       陆陆续续做了有一个月,期间因为各种技术问题被多次暂停,最关键的一次主要是因为存储容器使用的普通二叉树,在节点权重相同的情况下导致树高增高,在进行遍历的时候效率大大降低,甚至在使用递归的时候导致栈 ...

  7. 用WebCollector制作一个爬取《知乎》并进行问题精准抽取的爬虫(JAVA)

    简单介绍: WebCollector是一个无须配置.便于二次开发的JAVA爬虫框架(内核),它提供精简的的API.仅仅需少量代码就可以实现一个功能强大的爬虫. 怎样将WebCollector导入项目请 ...

  8. Selenium WebDriver TestNg Maven Eclipse java 简单实例

    环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...

  9. phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)

    1.  在使用phantomjs的时候需要下载phantomjs,网上有window版本和Linux版本.将phantomjs放在Linux上的指定位置之后(如下面的/home/tpl/phantom ...

随机推荐

  1. 下载的管理类MyDownloadManager

    import android.content.Intent; import android.net.Uri; import java.io.File; import java.io.FileOutpu ...

  2. Selenium+Python的开发环境搭建

    第一步:安装Python  https://www.python.org/ 注意:在cmd窗口中输入python,如果不是内部或者外部命令的需要配置环境变量Path ; 右击桌面我的电脑-->属 ...

  3. 代码实现:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.第二个完全数是28, //它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加, //编程找出1000以内的所有完数。

    import java.util.ArrayList; import java.util.List; //一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3. ...

  4. python获取豆瓣日记

    最近迷上了看了四个春天,迷上了饭叔的豆瓣日记,想全部抓取下来,简单了写了下面的脚本 import urllib.request import os from bs4 import BeautifulS ...

  5. github创建项目,并提交本地文件

    1.如图所示,不要点选"Initialize this repository with README",不然就看不到第二幅图的提示信息了 2.根据下面提示,初始化本地文件,然后上传

  6. Day06:抽象类、接口和内部类(上)

    JVAV中的常量 什么是常量? 常量就是不会变化的数值 为什么需要常量? 方便使用(调用)不会变化的数值 特性 不能修改 所有对象共享 常量一定是成员 定义 public static final 类 ...

  7. Tutorial : Implementing Django Formsets

    A step-by-step tutorial for setting up and testing a standard Django formset. I’ve noticed on #djang ...

  8. Docker 面试题(一)

    什么是Docker? Docker是一个容器化平台,它以容器的形式将您的应用程序及其所有依赖项打包在一起,以确保您的应用程序在任何环境中无缝运行. CI(持续集成)服务器的功能是什么? CI功能就是在 ...

  9. Win10无法安装.net framework 3.5出错提示无法安装以下功能该怎么办?

    在Windows操作系统中,.NET Framewor对今天应用程序的成功提供了的安全解决方案,它能强化两个安全模型间的平衡.在提供对资源的访问,以便以完成有用的工作,对应用程序的安全性作细致的控制以 ...

  10. Mysql-慢查询排查

    1.开启慢日志 2.使用show full processlist抓出慢查询语句 3.使用explain分析语句 4. set global profiling = ON