用于屏幕刮擦的Mozilla Parser

时间:2009-09-29 20:11:08

标签: java dom parsing screen-scraping mozilla

我正在编写一个应用程序,该应用程序接收页面的HTML代码并提取页面的某些元素(例如表格)并返回这些元素的html代码。我试图在java中使用Mozilla解析器来简化页面导航,但是我无法提取所需的html代码。

也许我的整个方法都错了,也就是Mozilla解析器,所以如果有更好的解决方案,我愿意接受建议

String html = ///what ever the code is

MozillaParser p = // instantiate parser


// pass in html to parse which creates a dom object
Document d = p.parse(html);

// get a list of all the form elements in the page
NodeList l =  d.getElementsByTagName("form");

// iterate through all forms
for(int i = 0; i < l.getLength(); i++){

    // get a form
    Node n = l.item(i);

    // print out the html code for just this form.
    // This is the portion I haven't figured out.
    // I just made up the innerHTML method, but thats
    // the end result I'm desiring, a way to just see
    // the html code for a particular node
    System.out.println( n.innerHTML() );
}

3 个答案:

答案 0 :(得分:1)

我使用htmlcleaner(http://htmlcleaner.sourceforge.net/)取得了一定程度的成功:它非常快,并且可以让您确定它应该是多么“严格”。尽管如此,我尽可能避免使用html抓取,因为所有相反的原因(通过REST或其他形式的API暴露的数据往往更可靠,更合法,更容易解析等等。)。

答案 1 :(得分:1)

Mozilla解析器在这里似乎有些过分,我已经使用Jericho取得了一些成功,只是你正在做的事情。

答案 2 :(得分:0)

我在Mozilla平台上使用Javascript编写了一个HTML包装器。我将代码打包到Firefox浏览器的两个扩展中。其中一个称为MetaStudio,是一种数据模式定义工具,可以在语义上对Web页面进行注释。另一种称为DataScraper的工具是从网页中提取数据片段并将其格式化为XML文件的工具。

所有源代码都是可读的。请转到http://www.gooseeker.com下载它们。