phanbedder使用實例

PhantomJS是個好東東,很多時候爬蟲需要用到,但是這個有windows、linux、mac的版本,想要跨平臺的話,需要自己再處理一下,幸好有了phanbedder幫我們做了這個事情,這樣就引一下jar包就完事了。

Selenium以及Phantom JS

Selenium是一個用於Web應用程序測試的工具。Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE、Mozilla Firefox、Chrome等。 Phantom JS是一個服務器端的 JavaScript API 的 WebKit。其支持各種Web標準: DOM 處理, CSS 選擇器, JSON, Canvas, 和 SVG。

maven

    <dependency>
      <groupId>net.anthavio</groupId>
      <artifactId>phanbedder-2.1.1</artifactId>
      <version>1.0.0</version>
    </dependency>

    <dependency>
      <groupId>com.github.detro.ghostdriver</groupId>
      <artifactId>phantomjsdriver</artifactId>
      <version>1.1.0</version>
    </dependency>

官網給的這個實際跑起來有點問題,報錯

Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 29 more

需要更換一下

<dependency>
            <groupId>net.anthavio</groupId>
            <artifactId>phanbedder-2.1.1</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>
        <dependency>
            <groupId>com.codeborne</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.1</version>
            <!-- this will _always_ be behind -->
            <exclusions>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-remote-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

驗證

        //Phanbedder to the rescue!
        File phantomjs = Phanbedder.unpack();
        DesiredCapabilities dcaps = new DesiredCapabilities();
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        driver.setLogLevel(Level.INFO);
        //Usual Selenium stuff...
        driver.get("https://www.google.com");
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Phanbedder");
        query.submit();

        System.out.println(driver.getTitle());
        driver.quit();

doc

  • phanbedder
  • Cannot use PhantomJS with selenium-java 2.44.0 #8088
  • selenium webdriver的各種driver
  • 數據抓取的藝術(一):Selenium+Phantomjs數據抓取環境配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章