基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)
2017年一直以來(lái)在公司負(fù)責(zé)爬蟲(chóng)項(xiàng)目相關(guān)工程,主要業(yè)務(wù)有預(yù)定、庫(kù)存、在開(kāi)發(fā)中也遇到很多問(wèn)題,隨手記錄一下,后續(xù)會(huì)持續(xù)更新。
chrome、firefox、phantomjs插件安裝和版本說(shuō)明
基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)
maven版本說(shuō)明
<!-- +++|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下載地址選擇本地系統(tǒng)對(duì)應(yīng)的chrome版本安裝,工程下面有一個(gè) 對(duì)應(yīng)的目錄是:Plugin/chromedriver_win32.zip,對(duì)應(yīng)chrmoe版本是Supports Chrome v60-62
直接運(yùn)行項(xiàng)目中示例
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配置插件是最簡(jiǎn)單的,linux上面只需要把插件換成linux版本即可
firefox
下載插件地址:geckodriver下載地址,選擇本地系統(tǒng)對(duì)應(yīng)的firefox版本安裝,工程下面有一個(gè) 對(duì)應(yīng)的目錄是:Plugin/geckodriver-v0.18.0-win64.zip,對(duì)應(yīng)firefox版本是Firefox Setup 50.0(64位)、其他版本沒(méi)有測(cè)試過(guò)
firefox下載地址、selenium-java版本和geckodriver版本更新迭代不一致,導(dǎo)致在搭建環(huán)境時(shí)很容易出現(xiàn)一系列問(wèn)題。
直接運(yùn)行項(xiàng)目中示例
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<>();
//獲取豆瓣標(biāo)簽
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);
//點(diǎn)擊小說(shuō)標(biāo)簽
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是沒(méi)有界面的,所以只需要下載插件即可。
直接運(yùn)行項(xiàng)目中示例
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();
}
}
}
}
到此這篇關(guān)于基于selenium-java封裝chrome、firefox、phantomjs實(shí)現(xiàn)爬蟲(chóng)的文章就介紹到這了,更多相關(guān)selenium java封裝爬蟲(chóng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot發(fā)送request請(qǐng)求的方式小結(jié)
在Java中,發(fā)送HTTP請(qǐng)求是常見(jiàn)需求,hutool工具包和RestTemplate類(lèi)是實(shí)現(xiàn)此功能的兩種主流方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
IDEA無(wú)法識(shí)別相關(guān)module模塊問(wèn)題的解決過(guò)程
這篇文章主要給大家介紹了關(guān)于IDEA無(wú)法識(shí)別相關(guān)module模塊問(wèn)題的解決過(guò)程,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用IDEA具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
springboot 設(shè)置局域網(wǎng)訪問(wèn)的實(shí)現(xiàn)步驟
Spring Boot是一個(gè)開(kāi)源Java-based框架,用于創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用,它旨在簡(jiǎn)化Spring應(yīng)用的初始搭建及開(kāi)發(fā)過(guò)程,通過(guò)提供各種自動(dòng)配置的starter包,Spring Boot使得項(xiàng)目配置變得簡(jiǎn)單快速,感興趣的朋友一起看看吧2024-02-02
完美解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問(wèn)題
在調(diào)用某個(gè)接口的時(shí)候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報(bào)錯(cuò)這個(gè)報(bào)錯(cuò),導(dǎo)致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問(wèn)題,感興趣的朋友一起看看吧2023-03-03
解決一個(gè)JSON反序列化問(wèn)題的辦法(空字符串變?yōu)榭占?
在平時(shí)的業(yè)務(wù)開(kāi)發(fā)中,經(jīng)常會(huì)有拿到一串序列化后的字符串要來(lái)反序列化,下面這篇文章主要給大家介紹了如何解決一個(gè)JSON反序列化問(wèn)題的相關(guān)資料,空字符串變?yōu)榭占?需要的朋友可以參考下2024-03-03
SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer解讀
這篇文章主要介紹了SpringCloud負(fù)載均衡spring-cloud-starter-loadbalancer使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶(hù)登錄功能全過(guò)程
在Java開(kāi)發(fā)過(guò)程中實(shí)現(xiàn)用戶(hù)的注冊(cè)功能是最基本的,這篇文章主要給大家介紹了關(guān)于Java?Web應(yīng)用小案例之實(shí)現(xiàn)用戶(hù)登錄功能的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

