溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-10-29 21:00:12 來源:億速云 閱讀:277 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)使用selenium-java封裝chrome、firefox、phantomjs實現(xiàn)爬蟲,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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)對應(yīng)的chrome版本安裝,工程下面有一個 對應(yīng)的目錄是:Plugin/chromedriver_win32.zip,對應(yīng)chrmoe版本是Supports Chrome v60-62

直接運(yùn)行項目中示例

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

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

直接運(yù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<>();
      //獲取豆瓣標(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)擊小說標(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是沒有界面的,所以只需要下載插件即可。

直接運(yùn)行項目中示例

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實現(xiàn)爬蟲,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI