類比ajax實現網路爬蟲——HtmlUnit

來源:互聯網
上載者:User

標籤:執行   byte   code   getopt   下載   select   oid   series   javascrip   

    最近在用Jsoup抓取某網站資料,可有些頁面是ajax請求動態產生的,去群裡問了一下,大神說類比ajax請求即可。去網上搜尋了一下,發現了這篇文章,拿過來先用著試試。

   轉帖如下:

 

 

網上關於網路爬蟲實現方式有很多種,但是很多都不支援Ajax,李兄說:類比才是王道。確實,如果能夠類比一個沒有介面的瀏覽器,還有什麼不能做到的呢? 關於解析Ajax網站的架構也有不少,我選擇了HtmlUnit,官方網站:http://htmlunit.sourceforge.net /,htmlunit可以說是一個Java版本的無介面瀏覽器,幾乎無所不能,而且很多東西都封裝得特別完美。這是這幾天來積累下來的心血,記錄一下。


package com.lanyotech.www.wordbank;import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.util.List; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.ScriptResult; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlOption; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlSelect; public class WorldBankCrawl {     private static String TARGET_URL = "http://databank.worldbank.org/ddp/home.do";         public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {         //類比一個瀏覽器         WebClient webClient = new WebClient();         //設定webClient的相關參數         webClient.setJavaScriptEnabled(true);         webClient.setCssEnabled(false);         webClient.setAjaxController(new NicelyResynchronizingAjaxController());         webClient.setTimeout(35000);         webClient.setThrowExceptionOnScriptError(false);         //類比瀏覽器開啟一個目標網址         HtmlPage rootPage= webClient.getPage(TARGET_URL);         //擷取第一個資料庫         HtmlSelect hs = (HtmlSelect) rootPage.getElementById("lstCubes");         //按要求選擇第一個資料庫         hs.getOption(0).setSelected(true);         //類比點擊Next按鈕,跳轉到第二個頁面         System.out.println("正在跳轉…");         //執行按鈕出發的js事件         ScriptResult sr = rootPage.executeJavaScript("javascript:setCubeData(2,-1,4,‘/ddp‘);");                 //跳轉到第二個頁面,選擇國家         HtmlPage countrySelect = (HtmlPage) sr.getNewPage();         //獲得包含全部國家資訊的選擇框頁面         HtmlPage framePage=(HtmlPage)countrySelect.getFrameByName("frmTree1″).getEnclosedPage();         //獲得selectAll按鈕,觸發js事件         framePage.executeJavaScript("javascript:TransferListAll(‘countrylst‘,‘countrylstselected‘,‘no‘);SetSelectedCount(‘countrylstselected‘,‘tdcount‘);");         //擷取Next按鈕,觸發js事件         ScriptResult electricityScriptResult = framePage.executeJavaScript("javascript:wrapperSetCube(‘/ddp‘)");                 System.out.println("正在跳轉…");         //跳轉到下一個頁面electricitySelect         HtmlPage electricitySelect = (HtmlPage) electricityScriptResult.getNewPage();         //獲得electricity選擇的iframe         HtmlPage electricityFrame = (HtmlPage) electricitySelect.getFrameByName("frmTree1″).getEnclosedPage();         //獲得選擇框         HtmlSelect seriesSelect = (HtmlSelect) electricityFrame.getElementById("countrylst");         //獲得所有的選擇框內容         List optionList = seriesSelect.getOptions();         //將指定的選項選中         optionList.get(1).setSelected(true);         //類比點擊select按鈕                       electricityFrame.executeJavaScript("javascript:TransferList(‘countrylst‘,‘countrylstselected‘,‘no‘);SetSelectedCount(‘countrylstselected‘,‘tdcount‘);");         //擷取選中後,下面的選擇框         HtmlSelect electricitySelected = (HtmlSelect) electricityFrame.getElementById("countrylstselected");         List list = electricitySelected.getOptions();         //類比點擊Next按鈕,跳轉到選擇時間的頁面         ScriptResult timeScriptResult = electricityFrame.executeJavaScript("javascript:wrapperSetCube(‘/ddp‘)");                 System.out.println("正在跳轉…");         HtmlPage timeSelectPage = (HtmlPage) timeScriptResult.getNewPage();         //擷取選中時間的選擇框         timeSelectPage = (HtmlPage) timeSelectPage.getFrameByName("frmTree1″).getEnclosedPage();         //選中所有的時間                timeSelectPage.executeJavaScript("javascript:TransferListAll(‘countrylst‘,‘countrylstselected‘,‘no‘);SetSelectedCount(‘countrylstselected‘,‘tdcount‘);");         //點擊Next按鈕         ScriptResult exportResult = timeSelectPage.executeJavaScript("javascript:wrapperSetCube(‘/ddp‘)");                 System.out.println("正在跳轉…");         //轉到export頁面         HtmlPage exportPage = (HtmlPage) exportResult.getNewPage();         //點擊頁面上的Export按鈕,進入下載頁面         ScriptResult downResult = exportPage.executeJavaScript("javascript:exportData(‘/ddp‘ ,‘EXT_BULK‘ ,‘WDI_Time=51||WDI_Series=1||WDI_Ctry=244||‘ );");                 System.out.println("正在跳轉…");         HtmlPage downLoadPage = (HtmlPage) downResult.getNewPage();         //點擊Excel表徵圖,開始下載         ScriptResult downLoadResult = downLoadPage.executeJavaScript("javascript:exportData(‘/ddp‘,‘BULKEXCEL‘);");         //下載Excel檔案         InputStream is = downLoadResult.getNewPage().getWebResponse().getContentAsStream();                 OutputStream fos = new FileOutputStream("d://test.xls");         byte[] buffer=new byte[1024*30];         int len=-1;         while((len=is.read(buffer))>0){             fos.write(buffer, 0, len);         }         fos.close();         fos.close();         System.out.println("Success!");     } } 

 

注釋:

  /**HtmlUnit請求web頁面*/          WebClient wc = new WebClient();          wc.getOptions().setJavaScriptEnabled(true); //啟用JS解譯器,預設為true          wc.getOptions().setCssEnabled(false); //禁用css支援          wc.getOptions().setThrowExceptionOnScriptError(false); //js運行錯誤時,是否拋出異常          wc.getOptions().setTimeout(10000); //設定連線逾時時間 ,這裡是10S。如果為0,則無限期等待          HtmlPage page = wc.getPage("http://cq.qq.com/baoliao/detail.htm?294064");          String pageXml = page.asXml(); //以xml的形式擷取響應文本 


類比ajax實現網路爬蟲——HtmlUnit

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.