小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

最近用htmlunit做網(wǎng)絡(luò)爬蟲(chóng) 遇到拿不到初始化js加載的數(shù)據(jù)的問(wèn)題 最近解決了 寫(xiě)個(gè)簡(jiǎn)單的例子

 benham 2018-08-22

public static void main(String[] args) throws Exception {      

String url="www.xxx.com";

        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
        //設(shè)置webClient的相關(guān)參數(shù)
        webClient.setJavaScriptEnabled(true);
        webClient.setCssEnabled(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.setTimeout(50000);
        webClient.setThrowExceptionOnScriptError(false);
        //模擬瀏覽器打開(kāi)一個(gè)目標(biāo)網(wǎng)址
            HtmlPage rootPage= webClient.getPage(url);
            System.out.println("為了獲取js執(zhí)行的數(shù)據(jù) 線(xiàn)程開(kāi)始沉睡等待");
            Thread.sleep(30000);//注意 這步非常重要 因?yàn)轫?yè)面的初始化js加載也是需要時(shí)間的 具體時(shí)間可以自己調(diào)
            System.out.println("線(xiàn)程結(jié)束沉睡");

            String html = rootPage.asXml();

            System.out.println(html);

//后面是一個(gè)擴(kuò)展  ,你前面的沉睡時(shí)間可以設(shè)短點(diǎn) 然后判斷 抓過(guò)來(lái)的數(shù)據(jù)有沒(méi)有你需要的 如果有  那就解析唄 我用的是Jsoup這個(gè)解析的 所需要的jar

//在我的資源里面有 地址http://download.csdn.net/detail/caiselangren/5743399     自己下了引人就行

        Document doc = Jsoup.parse(html);
        Element content = doc.getElementById("XXX");//這個(gè)是你要獲取內(nèi)容的ID 當(dāng)然 你也可以用class或者元素名 或者name什么的去獲取
        Elements Numbers = null;
        Elements Numbers_price = null;
        Elements Numbers_detail = null;
        if(content!=null){
            Numbers=content.getElementsByAttributeValue("class", "XXX");
        }else{
            //數(shù)據(jù)沒(méi)獲取到
            for(int i=0;i<20;i++){
                System.out.println("數(shù)據(jù)沒(méi)獲取到延長(zhǎng)沉睡5秒");
                Thread.sleep(5000);
                rootPage = webClient.getPage(url);
                 doc = Jsoup.parse(rootPage.asXml());
                 content = doc.getElementById("XXX");
                 if(content!=null){
                     Numbers=content.getElementsByAttributeValue("class", "XXX");
                     break;
                 }
                 if(i==19){
                     System.out.println("數(shù)據(jù)超過(guò)查詢(xún)時(shí)間,停止查詢(xún)");
                 }
            }
            
        }

if(Numbers!=null){

   for(int i=0;i<Numbers.size();i++){

Element Number = Numbers.get(i);

System.out.println(Number.html());

}

}



}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多