爬蟲 ajax網頁(Cobra)

來源:互聯網
上載者:User
http://lobobrowser.org/cobra.jsp

有js邏輯的頁面,對網路爬蟲的資訊抓取工作造成了很大障礙。DOM樹,只有執行了js的邏輯才可以完整的呈現。而有的時候,有要對js修改後的 dom樹進行解析。在搜尋了大量資料後,發現了一個開源的項目cobra。cobra支援JavaScript引擎,其內建的JavaScript引擎是 mozilla下的 rhino,利用rhino的API,實現了對嵌入在html的JavaScript的解釋執行。測試案例:

js.html 

<html> 

<title>test javascript</title> 

<script language="javascript"> 

var go = function(){ 



document.getElementById("gg").innerHTML="google"; 



</script> 

<body onLoad="javascript:go();"> 

<a id = "gg" onClick="javascript:go();" href="#">baidu</a> 

</body> 

</html> 

Test.java 

package net.cooleagle.test.cobra;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.net.URL;




import org.lobobrowser.html.UserAgentContext;

import org.lobobrowser.html.domimpl.HTMLDocumentImpl;

import org.lobobrowser.html.parser.DocumentBuilderImpl;

import org.lobobrowser.html.parser.InputSourceImpl;

import org.lobobrowser.html.test.SimpleUserAgentContext;

import org.w3c.dom.Document;

import org.w3c.dom.Element;




public class Test{

private static final String TEST_URI = "http://localhost/js.html";



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

UserAgentContext uacontext = new SimpleUserAgentContext();

DocumentBuilderImpl builder = new DocumentBuilderImpl(uacontext);

URL url = new URL(TEST_URI);

InputStream in = url.openConnection().getInputStream();

try {

Reader reader = new InputStreamReader(in, "ISO-8859-1");

InputSourceImpl inputSource = new InputSourceImpl(reader, TEST_URI);

Document d = builder.parse(inputSource);

HTMLDocumentImpl document = (HTMLDocumentImpl) d;

Element ele = document.getElementById("gg");

System.out.println(ele.getTextContent());



} finally {

in.close();

}

}

}

執行結果: 

google 

測試成功。

============================================

I originally used JRex, a Java wrapper for the Mozilla Gecko layout engine, to render HTML pages. I was looking for a better engine for extracting the HTML of rendered pages and found the Cobra Toolkit that is part of the Lobo Project. This project includes the Cobra Toolkit that renders HTML and the LoboBrowser built on this toolkit. The code is pure Java.

My initial comparison of JRex and Cobra found the following salient facts:

  • JRex seems to be an abandoned project while the Lobo Project is active. The forums for this project are more active than for JRex.
  • While JRex appears to be abandoned, Gecko is a world-class rendering engine. Cobra still seems to be in development.
  • JRex crashes the Java JVM when loading certain pages, and Cobra does not.
  • Cobra can be run headless while JRex/Gecko cannot. Cobra seems faster since it doesn't have to actually render the HTML page to a graphic context.
  • By default, JRex/Gecko includes a Flash plug-in while Cobra does not. (Since the plug-in mechanism for the LoboBrowser requires Java code, plug-ins for other browsers will not work. Until a Java Flash plug-in is available, Cobra will not handle Flash.) The JavaScript in some pages will cause a modified page to be loaded if Flash isn't present. In some data mining tasks, being able to examine the <OBJECT> and <EMBED> tags is useful and might not be available in Cobra unless a plug-in for Flash is installed.
  • JRex/Gecko seems to handle less well-formed HTML than Cobra. A missing <HTML> or <HEAD> tag can cause Cobra to quit before building the complete DOM. But since the LoboBrowser does properly render one of my test pages that Cobra fails on, perhaps this is less of a problem than I think.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.