java phantomjs 爬虫_使用selenium-java封装chrome、firefox、phantomjs实现爬虫

使用selenium-java封装chrome、firefox、phantomjs实现爬虫

发布时间:2020-10-29 21:00:12

来源:亿速云

阅读:87

作者:Leah

本篇文章给大家分享的是有关使用selenium-java封装chrome、firefox、phantomjs实现爬虫,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

maven版本说明

org.seleniumhq.selenium

selenium-java

3.5.1

com.github.detro.ghostdriver

phantomjsdriver

1.1.0

chrome插件配置

下载地址:chromedriver下载地址选择本地系统对应的chrome版本安装,工程下面有一个 对应的目录是:Plugin/chromedriver_win32.zip,对应chrmoe版本是Supports Chrome v60-62

直接运行项目中示例

public class ChromeTest {

public static void main(String[] args) {

WebDriver webDriver = null;

try {

webDriver = WebDriverUtil.createChromeWebDriver("D:\\webdrvier\\chromedriver.exe");//修改路径

webDriver.get("https://www.baidu.com/");

System.out.println(webDriver.getTitle());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (webDriver != null) {

webDriver.close();

}

}

}

}

chrome配置插件是最简单的,linux上面只需要把插件换成linux版本即可

firefox

下载插件地址:geckodriver下载地址,选择本地系统对应的firefox版本安装,工程下面有一个 对应的目录是:Plugin/geckodriver-v0.18.0-win64.zip,对应firefox版本是Firefox Setup 50.0(64位)、其他版本没有测试过

firefox下载地址、selenium-java版本和geckodriver版本更新迭代不一致,导致在搭建环境时很容易出现一系列问题。

直接运行项目中示例

public class FireFoxTest {

public static void main(String[] args) {

WebDriver webDriver = null;

try {

webDriver = WebDriverUtil.createFirefoxWebDriver("D:\\webdrvier\\Firefox\\geckodriver_18.exe");

webDriver.get("https://book.douban.com/tag/");

Set tagSet = new HashSet<>();

//获取豆瓣标签

List divWebElement = webDriver.findElements(By.cssSelector("#content > div > div.article > div:nth-child(2) > div"));

for (WebElement webElement : divWebElement) {

List aWebElement = webElement.findElements(By.cssSelector("a"));

for (WebElement element : aWebElement) {

tagSet.add(element.getText());

}

}

System.out.println(tagSet);

//点击小说标签

WebElement webElement = webDriver.findElement(By.cssSelector("#content > div > div.article > div:nth-child(2) > div:nth-child(1) > table > tbody > tr:nth-child(1) > td:nth-child(1) > a"));

webElement.click();

System.out.println(webDriver.getTitle());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (webDriver != null) {

webDriver.quit();

webDriver.close();

}

}

}

}

phantomjs

下载插件地址phantomjs插件地址1、phantomjs插件地址2、下载有些慢。phantomjs是没有界面的,所以只需要下载插件即可。

直接运行项目中示例

public class PhantomjsTest {

public static void main(String[] args) {

WebDriver webDriver = null;

try {

webDriver = WebDriverUtil.createPhantomjsWebDriver("D:/webdrvier/phantomjs-1.9.8-windows/phantomjs.exe");

webDriver.get("https://www.baidu.com/");

System.out.println(webDriver.getTitle());

} catch (Exception e) {

e.printStackTrace();

} finally {

if (webDriver != null) {

webDriver.close();

}

}

}

}

以上就是使用selenium-java封装chrome、firefox、phantomjs实现爬虫,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值