乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       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);
              //模擬瀏覽器打開一個目標(biāo)網(wǎng)址
                  HtmlPage rootPage= webClient.getPage(url);
                  System.out.println("為了獲取js執(zhí)行的數(shù)據(jù) 線程開始沉睡等待");
                  Thread.sleep(30000);//注意 這步非常重要 因?yàn)轫撁娴某跏蓟痡s加載也是需要時間的 具體時間可以自己調(diào)
                  System.out.println("線程結(jié)束沉睡");

                  String html = rootPage.asXml();

                  System.out.println(html);

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

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

              Document doc = Jsoup.parse(html);
              Element content = doc.getElementById("XXX");//這個是你要獲取內(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ù)沒獲取到
                  for(int i=0;i<20;i++){
                      System.out.println("數(shù)據(jù)沒獲取到延長沉睡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ù)超過查詢時間,停止查詢");
                       }
                  }
                  
              }

      if(Numbers!=null){

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

      Element Number = Numbers.get(i);

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

      }

      }



      }

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多