JAVA獲取異步(ajax)請求數據

前言
2016年大四上學期實習,參與爬蟲系統開發,公司也遇到了如何獲取ajax,vue,angularJS渲染的網頁的問題,于是我便開始在Baidu,Google上尋覓了幾天,獲得了以下兩種較為穩定方法。

方法一 Http analyzer + httpclient

獲取一個網站某個數據區域的精準的數據,那么推薦用Http analyzer分析請求及參數,用httpclient模擬請求(注意如果是多個請求,那么必須用同一個httpclient對象去執行,因為高版本的HttpClient會自動保持Cookie信息),一般都能夠獲得請求返回的json數據,當然,如果網站有做反爬蟲處理或者其他的一些處理,那就困難了。

方法二:Selenium+PhantomJS

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.codeborne/phantomjsdriver -->
    <dependency>
        <groupId>com.codeborne</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.2.1</version>
    </dependency>

4 編碼,為了進行對比獲取ajax數據差異,使用httpclient與之對比
Selenium+PhantomJS:

public class WebdriverDownloader {
    static {
       //phantomJsPath=E:/dev/phantomjs/bin/phantomjs.exe
        String phantomJsPath = PropertyResourceBundle.getBundle("webdriver").getString("phantomJsPath");
           System.setProperty("phantomjs.binary.path",phantomJsPath);
    }
    /**
     * download html
     * @param webDriver
     * @param url
     * @return
     */
    public static String download(WebDriver webDriver,String url){
        webDriver.get(url);
        WebElement webElement = webDriver.findElement(By.xpath("/html"));
        return webElement.getAttribute("outerHTML");
    }
    public static WebDriver create(){
        WebDriver webDriver=new PhantomJSDriver();
        /**Specifies the amount of time the driver should wait when searching for an element if it is
         * not immediately present.*/
        webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        return webDriver;
    }

    /**
     *
     * close window,must quit first,then close
     */
    public static void close(WebDriver driver)  {
        driver.quit();
        driver.close();
    }
     public static void main(String []args){
         String url = "http://www.tianyancha.com/search?key=%E7%99%BE%E5%BA%A6&checkFrom=searchBox";
         WebDriver webDriver = WebdriverDownloader.create();
        String html = WebdriverDownloader.download(webDriver, url);
        // WebdriverDownloader.close(webDriver);
         System.out.println(html);
     }

HttpClient 代碼:

public class HttpDownload {
    public static String  download(String url) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
       return EntityUtils.toString(client.execute(new HttpGet(url)).getEntity());
    }

    public static void main(String []args) throws IOException {
        String url = "http://www.tianyancha.com/search?key=%E7%99%BE%E5%BA%A6&checkFrom=searchBox";
        String html = HttpDownload.download(url);
        System.out.println(html);
    }
}```

Selenium+PhantomJS:
由于源碼過長,截取部分源碼和HttpClient下載的源碼做對比
                                      style="margin-right: 2px;"> 相關的人</span><img
                                    style="height: 14px; margin-bottom: 3px;"
                                    src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                    style="margin-left: 5px;" class="search_last_color"
                                    ng-bind-html="node.humanNames"></span></span>
                            </p> --></div><!--body--><div class="row" style="margin-left: 0; margin-right: 0"><div class="search_row_new"><div class="title overflow-width" style="padding-left: 0">法定代表人: <span title="黃金龍" ng-bind-html="node.legalPersonName?node.legalPersonName:'未公開' | trustHtml" class="ng-binding">黃金龍</span></div><!-- <div class="title overflow-width">
                                    行業:<span title="{{node.industry?node.industry:'未公開'}}">{{node.industry?node.industry:'未公開'}}</span>
                                </div> --><div class="title overflow-width">注冊資本:<span title="6000.000000萬人民幣" class="ng-binding">6000.000000萬人民幣
                            <span ng-if="node.trademarks" style="margin-right: 20px;"><span
                                    class="c3" style="margin-right: 2px;">品牌</span><img alt="|"
                                                                                        style="height: 14px; margin-bottom: 3px;"
                                                                                        src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                    style="margin-left: 5px;" class="search_last_color"
                                    ng-bind-html="node.trademarks | trustHtml"></span></span> <span
                                    ng-if="node.humanNames"><span class="c3"
                                                                  style="margin-right: 2px;"> 相關的人</span><img
                                    style="height: 14px; margin-bottom: 3px;"
                                    src="http://static.tianyancha.com/wap/images/cutting-line.png" /><span
                                    style="margin-left: 5px;" class="search_last_color"
                                    ng-bind-html="node.humanNames"></span></span>
                            </p> --></div><!--body--><div class="row" style="margin-left: 0; margin-right: 0"><div class="search_row_new"><div class="title overflow-width" style="padding-left: 0">法定代表人: <span title="趙坤" ng-bind-html="node.legalPersonName?node.legalPersonName:'未公開' | trustHtml" class="ng-binding">趙坤</span></div><!-- <div class="title overflow-width">
                                    行業:<span title="{{node.industry?node.industry:'未公開'}}">{{node.industry?node.industry:'未公開'}}</span>
                                </div> --><div class="title overflow-width">注冊資本:<span title="1000萬人民幣" class="ng-binding">1000萬人民幣</span></div><div class="title overflow-width" style="border-right: none">注冊時間:<span title="2013-10-23 " clnode.bondType --><!-- <p ng-if="node.humanNames||node.trademarks">
                                    ```

可以看到Selenium+PhantomJS可以獲取到動態邦定的數據,在源碼中可以看到ng-binding,說明網站前端使用了angurlarJS。當然獲取到了源碼提取出來的信息才是有的,一般都可以使用xpath(使用xpath推薦使用xpath heper這個chrome瀏覽器插件可以幫助提高寫xpath的效率),jsoup這些進行源碼的解析,此文不再贅述。

總結:
直接用Java獲取ajax或者使用vue.js,angularJS進行前端渲染的頁面往往得不到渲染的網頁,因為這些網頁都是需要執行JS進行渲染的。
使用HtmlUnit執行JS易出錯,我用的時候基本沒成功過,只有Selenium+瀏覽器的方案是目前相對穩妥的,但是ChromeDriver和FireFoxDriver在啟動時都會彈出一個對話框,這樣的體驗并不好,而PhantomJS卻可以在后臺運行。
網上Java使用PhantomJS組合方案時一般都會寫一段js讓PhantomJS去load網頁,其實使用Selenium+PhantomJS那段js就不用寫了。并且還可以使用Seleinum提供的API進行模擬點擊。
注意:上面的代碼僅用于測試,如果要投入生產需要考慮很多,在創建Webdriver的過程是一個非常耗時的過程,根據使用場合可以將Webdriver進行池化(可以使用apache的Commons Pool
注:實習結束了,也沒做爬蟲了,寫個文檔紀念一下。文中有很多參考了別人的博客,但是時間太久記不住是哪些博客了,敬請見諒。如果觀點有誤,歡迎指正。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • WebDriver基礎 歡迎閱讀WebDriver基礎講義。本篇講義將會重點介紹Selenium WebDrive...
    厲鉚兄閱讀 5,521評論 3 22
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,765評論 18 399
  • 2016.6.7高考第一天,看到空間里對于高考作文的吐槽各種神奇葩,神吐槽,我看了看這感覺挺無奈的…………現在是凌...
    默落_閱讀 134評論 0 0
  • [幸福人兒]20170824學習力6期踐行記錄Day101 ?鵝媽媽聽了10分鐘×2次。 ?幼兒視覺游戲(食物)一...
    金大地閱讀 139評論 0 0