基于selenium-java封装chrome、firefox、phantomjs实现爬虫
#代码知识 发布时间: 2026-01-12
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();
}
}
}
} 代码知识SEO
上一篇 : react antd表格中渲染一张或多张图片的实例
下一篇 : MySQL 8.0.19安装详细教程(windows 64位)
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!