基于selenium-java封装chrome、firefox、phantomjs实现爬虫
作者:zhouhao1256 时间:2022-04-07 19:04:28
2017年一直以来在公司负责爬虫项目相关工程,主要业务有预定、库存、在开发中也遇到很多问题,随手记录一下,后续会持续更新。
chrome、firefox、phantomjs插件安装和版本说明
基于selenium-java封装chrome、firefox、phantomjs实现爬虫
项目下载地址
maven版本说明
<!-- +++|selenium|+++ -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.1</version>
</dependency>
<!-- +++|phantomjsdriver|+++ -->
<dependency>
<groupId>com.github.detro.ghostdriver</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.1.0</version>
</dependency>
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<String> tagSet = new HashSet<>();
//获取豆瓣标签
List<WebElement> divWebElement = webDriver.findElements(By.cssSelector("#content > div > div.article > div:nth-child(2) > div"));
for (WebElement webElement : divWebElement) {
List<WebElement> 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();
}
}
}
}
来源:https://blog.csdn.net/zhouhao1256/article/details/78543286
标签:selenium,java,chrome,firefox,phantomjs,爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java解析json数组方式
2023-08-10 17:22:14
![](https://img.aspxhome.com/file/2023/8/58188_0s.png)
详解spring mvc中url-pattern的写法
2023-11-11 07:30:58
java多线程实现下载图片并压缩
2023-01-17 22:28:34
完美解决Android Studio集成crashlytics后无法编译的问题
2023-06-23 16:49:07
![](https://img.aspxhome.com/file/2023/8/82388_0s.jpg)
Java流程控制之循环结构for,增强for循环
2023-11-03 10:45:20
![](https://img.aspxhome.com/file/2023/4/58834_0s.jpg)
Java实现按行读取大文件
2022-11-05 13:56:57
Java多线程回调方法实例解析
2023-11-04 01:40:01
![](https://img.aspxhome.com/file/2023/2/59002_0s.png)
关于@GetMapping和@GetMapping(value=““)的区别
2023-11-27 11:07:37
![](https://img.aspxhome.com/file/2023/2/59992_0s.png)
SpringBoot整合Web开发之Json数据返回的实现
2023-04-27 05:06:51
![](https://img.aspxhome.com/file/2023/9/61639_0s.png)
springcloud feign传输List的坑及解决
2023-06-20 18:31:57
RocketMQ源码解析broker 启动流程
2022-12-25 10:50:54
详解Java中的实例初始化块(IIB)
2023-06-08 08:35:46
Android 沉浸式状态栏及悬浮效果
2023-07-29 23:03:15
![](https://img.aspxhome.com/file/2023/4/86014_0s.gif)
Spring Boot学习入门之AOP处理请求详解
2023-11-27 10:55:17
Java最简洁数据结构之冒泡排序快速理解
2023-12-01 06:31:52
![](https://img.aspxhome.com/file/2023/8/60278_0s.png)
MyBatis插入Insert、InsertSelective的区别及使用心得
2023-08-25 04:34:28
浅析Java多线程同步synchronized
2023-05-20 15:52:29
android自动生成dimens适配文件的图文教程详解(无需Java工具类)
2023-07-17 12:12:30
![](https://img.aspxhome.com/file/2023/1/69721_0s.png)
通过实例解析Spring Ioc项目实现过程
2023-11-24 10:12:33
![](https://img.aspxhome.com/file/2023/5/59745_0s.png)
Opencv光流运动物体追踪详解
2023-06-21 11:55:31
![](https://img.aspxhome.com/file/2023/4/91994_0s.jpg)