【问题标题】:Mozilla Parser for screen scrapingMozilla Parser 用于屏幕抓取
【发布时间】:2010-12-02 10:53:35
【问题描述】:

我正在编写一个应用程序,它接收页面的 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() );
}

【问题讨论】:

  • 使用 mozilla 解析引擎对于您正在做的事情来说是相当重量级的。只是一个观察。

标签: java dom parsing screen-scraping mozilla


【解决方案1】:

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

【讨论】:

  • 是的,这看起来是个不错的选择。我觉得 Mozilla 有点过分了
【解决方案2】:

我使用 htmlcleaner (http://htmlcleaner.sourceforge.net/) 取得了一定程度的成功:它非常快,并且可以选择让您确定它应该有多“严格”。不过,出于所有显而易见的原因(通过 REST 或其他形式的 API 公开的数据往往更可靠、合法、更易于解析等),我尽量避免 html 抓取。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 2016-01-11
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 2019-01-17
      相关资源
      最近更新 更多