</title> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <style type="text/CSS"> a { line-height: 30px; margin: 20px; } </style> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript"> var datas = [ { href : "http://news.QQ.com/a/20140416/017800.htm", title : "高校一保安長(zhǎng)相酷似作家莫言" }, { href : "http://news.qq.com/a/20140416/015167.htm", title : "男子單臂托舉懸空女半小時(shí)" }, { href : "http://news.qq.com/a/20140416/013808.htm", title : "女子上門(mén)討房租遭強(qiáng)奸拍裸照" }, { href : "http://news.qq.com/a/20140416/016805.htm", title : "澳洲駱駝愛(ài)喝冰鎮(zhèn)啤酒解暑" } ]; window.onload = function() { var infos = document.getElementById("infos"); for( var i = 0 ; i < datas.length ; i++) { var a = document.createElement("a"); a.href = datas[i].href ; a.innerText = datas[i].title; infos.appendChild(a); infos.appendChild(document.createElement("br")) } } </script> </head> <body> Hello Main HttpUnit! <br> <div id="infos" style="width: 60%; border: 1px solid green; border-radius: 10px; margin: 0 auto;"> </div> </body> </html> 頁(yè)面上觀察是這樣顯示的:
我們審查元素:
如果你看到這樣的頁(yè)面,你會(huì)覺(jué)得拿Jsoup來(lái)抓取,簡(jiǎn)直就是呵呵,小菜一疊,于是我們寫(xiě)了這樣的代碼:
@Test ublic void testUserJsoup() { try { Document doc = Jsoup.connect( "http://localhost:8080/strurts2fileupload/main.html") .timeout(5000).get(); Elements links = doc.body().getElementsByTag("a"); for (Element link : links) { System.out.PRintln(link.text() + " " + link.attr("href")); } } catch (IOException e) { e.printStackTrace(); } 你會(huì)覺(jué)得就這幾行代碼,輕輕松松搞定,快快樂(lè)樂(lè)下班。于是運(yùn)行發(fā)現(xiàn),其實(shí)什么的抓取不到。于是我們?cè)倩氐巾?yè)面,打開(kāi)頁(yè)面源代碼,也就是上面的HTML代碼,你恍然大悟,我靠,body里面根本沒(méi)有數(shù)據(jù),難怪抓不到。這就是Jsoup的不足,如果Jsoup去抓取的頁(yè)面的數(shù)據(jù),全都是頁(yè)面加載完成后,Ajax獲取形成的,是抓取不到的。
下面給大家推薦另一個(gè)開(kāi)源項(xiàng)目:HttpUnit,看名字是用于測(cè)試的,但是用來(lái)抓取數(shù)據(jù)也不錯(cuò)
我們開(kāi)始編寫(xiě)類(lèi)似Jsoup的代碼:
@Test public void testUserHttpUnit() throws FailingHttpStatusCodeException, MalformedURLException, IOException { /** HtmlUnit請(qǐng)求web頁(yè)面 */ WebClient wc = new WebClient(BrowserVersion.Chrome); wc.getOptions().setUseInsecureSSL(true); wc.getOptions().setJavascriptEnabled(true); // 啟用JS解釋器,默認(rèn)為true wc.getOptions().setCssEnabled(false); // 禁用css支持 wc.getOptions().setThrowExceptionOnScriptError(false); // js運(yùn)行錯(cuò)誤時(shí),是否拋出異常 wc.getOptions().setTimeout(100000); // 設(shè)置連接超時(shí)時(shí)間 ,這里是10S。如果為0,則無(wú)限期等待 wc.getOptions().setDoNotTrackEnabled(false); HtmlPage page = wc .getPage("http://localhost:8080/strurts2fileupload/main.html"); DomNodeList<DomElement> links = page.getElementsByTagName("a"); for (DomElement link : links) { System.out .println(link.asText() + " " + link.getAttribute("href")); } } 再看一下運(yùn)行結(jié)果:
完美解決,HttpUnit其實(shí)就相當(dāng)于一個(gè)沒(méi)有UI的瀏覽器,它可以讓頁(yè)面上的js執(zhí)行完成后,再抓取信息,具體的介紹,google一下就行。主要給大家介紹一種方案!
如果你覺(jué)得這篇文章對(duì)你有用,就頂一個(gè)~
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注