欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于selenium-java封裝chrome、firefox、phantomjs實現(xiàn)爬蟲

 更新時間:2020年10月28日 11:40:05   作者:zhouhao1256  
這篇文章主要介紹了基于selenium-java封裝chrome、firefox、phantomjs實現(xiàn)爬蟲,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

2017年一直以來在公司負責爬蟲項目相關(guān)工程,主要業(yè)務有預定、庫存、在開發(fā)中也遇到很多問題,隨手記錄一下,后續(xù)會持續(xù)更新。

chrome、firefox、phantomjs插件安裝和版本說明
基于selenium-java封裝chrome、firefox、phantomjs實現(xiàn)爬蟲

項目下載地址

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下載地址選擇本地系統(tǒng)對應的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下載地址,選擇本地系統(tǒng)對應的firefox版本安裝,工程下面有一個 對應的目錄是:Plugin/geckodriver-v0.18.0-win64.zip,對應firefox版本是Firefox Setup 50.0(64位)、其他版本沒有測試過

firefox下載地址selenium-java版本和geckodriver版本更新迭代不一致,導致在搭建環(huán)境時很容易出現(xiàn)一系列問題。

直接運行項目中示例

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();
    }
  }
}
}

到此這篇關(guān)于基于selenium-java封裝chrome、firefox、phantomjs實現(xiàn)爬蟲的文章就介紹到這了,更多相關(guān)selenium java封裝爬蟲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Maven中dependencyManagement管理項目依賴項

    Maven中dependencyManagement管理項目依賴項

    在開發(fā)?Java?項目時,管理和協(xié)調(diào)依賴項的版本號是一項重要而繁瑣的任務,本文主要介紹了Maven中dependencyManagement管理項目依賴項,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • 淺談DetachedCriteria和Criteria的使用方法(必看)

    淺談DetachedCriteria和Criteria的使用方法(必看)

    下面小編就為大家?guī)硪黄獪\談DetachedCriteria和Criteria的使用方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java實現(xiàn)向Word文檔添加文檔屬性

    Java實現(xiàn)向Word文檔添加文檔屬性

    這篇文章主要介紹了Java實現(xiàn)向Word文檔添加文檔屬性的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 基于Java實現(xiàn)計數(shù)排序,桶排序和基數(shù)排序

    基于Java實現(xiàn)計數(shù)排序,桶排序和基數(shù)排序

    這篇文章主要為大家詳細介紹了計數(shù)排序,桶排序和基數(shù)排序的多種語言的實現(xiàn)(JavaScript、Python、Go語言、Java),感興趣的小伙伴可以了解一下
    2022-12-12
  • RestTemplate使用之如何設置請求頭、請求體

    RestTemplate使用之如何設置請求頭、請求體

    這篇文章主要介紹了RestTemplate使用之如何設置請求頭、請求體問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java Scanner類及其方法使用圖解

    Java Scanner類及其方法使用圖解

    這篇文章主要介紹了Java Scanner類及其方法使用圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot進行Web開發(fā)的實現(xiàn)

    SpringBoot進行Web開發(fā)的實現(xiàn)

    Spring?Boot讓我們可以快速構(gòu)建項目并運行web應用,大大簡化了Spring的復雜配置,本文主要介紹了SpringBoot進行Web開發(fā)的實現(xiàn),感興趣的可以了解一下
    2023-10-10
  • SpringBoot發(fā)送郵件功能 驗證碼5分鐘過期

    SpringBoot發(fā)送郵件功能 驗證碼5分鐘過期

    這篇文章主要為大家詳細介紹了SpringBoot發(fā)送郵件功能,驗證碼5分鐘過期,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 詳解Java中ExceptionInInitializer錯誤的解決方法

    詳解Java中ExceptionInInitializer錯誤的解決方法

    ExceptionInInitializerError 是 Java 中的未經(jīng)檢查的異常,它是 Error 類的子類, 它屬于運行時異常的類別,下面我們就來看看它的具體解決方法吧
    2024-02-02
  • spring MVC實踐需要注意的地方

    spring MVC實踐需要注意的地方

    這篇文章主要介紹了spring MVC實踐需要注意的地方,幫助大家更好的理解和學習使用spring MVC,感興趣的朋友可以了解下
    2021-03-03

最新評論