前言
2016年大四上學期實習,參與爬蟲系統開發,公司也遇到了如何獲取ajax,vue,angularJS渲染的網頁的問題,于是我便開始在Baidu,Google上尋覓了幾天,獲得了以下兩種較為穩定方法。
方法一 Http analyzer + httpclient
獲取一個網站某個數據區域的精準的數據,那么推薦用Http analyzer分析請求及參數,用httpclient模擬請求(注意如果是多個請求,那么必須用同一個httpclient對象去執行,因為高版本的HttpClient會自動保持Cookie信息),一般都能夠獲得請求返回的json數據,當然,如果網站有做反爬蟲處理或者其他的一些處理,那就困難了。
方法二:Selenium+PhantomJS
Selenium,PhantomJS是什么?
Selenium:用于Web應用程序測試的工具
PhantomJS: PhantomJS是一個基于webkit的javascript api接下來直接配置項目需要的環境
1 下載PhantomJS支持windows和linux
2 將下載的文件解壓到指定目錄(注我的是 e:/dev)
3 新建java項目所需的依賴如下:
<!-- 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)
注:實習結束了,也沒做爬蟲了,寫個文檔紀念一下。文中有很多參考了別人的博客,但是時間太久記不住是哪些博客了,敬請見諒。如果觀點有誤,歡迎指正。