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); //模擬瀏覽器打開一個(gè)目標(biāo)網(wǎng)址 HtmlPage rootPage= webClient.getPage(url); System.out.println("為了獲取js執(zhí)行的數(shù)據(jù) 線程開始沉睡等待"); Thread.sleep(30000);//注意 這步非常重要 因?yàn)轫撁娴某跏蓟痡s加載也是需要時(shí)間的 具體時(shí)間可以自己調(diào) System.out.println("線程結(jié)束沉睡"); String html = rootPage.asXml(); System.out.println(html); //后面是一個(gè)擴(kuò)展 ,你前面的沉睡時(shí)間可以設(shè)短點(diǎn) 然后判斷 抓過來的數(shù)據(jù)有沒有你需要的 如果有 那就解析唄 我用的是Jsoup這個(gè)解析的 所需要的jar //在我的資源里面有 地址http://download.csdn.net/detail/caiselangren/5743399 自己下了引人就行
Document doc = Jsoup.parse(html); if(Numbers!=null){ for(int i=0;i<Numbers.size();i++){ Element Number = Numbers.get(i); System.out.println(Number.html()); } } } |
|