Java 用于屏幕抓取的Mozilla解析器
我正在编写一个应用程序,它接收页面的HTML代码,提取页面的某些元素(如表),并返回这些元素的HTML代码。我试图在java中使用Mozilla解析器来简化页面导航,但是提取所需的html代码时遇到了问题 也许我的整个方法都是错误的,也就是Mozilla解析器,所以如果有更好的解决方案,我愿意接受建议Java 用于屏幕抓取的Mozilla解析器,java,dom,parsing,screen-scraping,mozilla,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
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() );
}
String html=///代码是什么
MozillaParser p=//实例化解析器
//传入html以解析创建dom对象的对象
文档d=p.parse(html);
//获取页面中所有表单元素的列表
NodeList l=d.getElementsByTagName(“表单”);
//遍历所有表单
对于(int i=0;i
我已经对使用htmlcleaner()的成功进行了衡量:它非常快速,并且有一些选项可以让您确定它应该有多“严格”。尽管如此,出于种种令人厌恶的原因(通过REST或其他形式的API公开的数据往往更可靠、更合法、更易于解析等),我尽量避免在任何可能的情况下删除html。Mozilla解析器在这里似乎有些过火了,我已经成功地使用了您正在做的事情。我已经在Mozilla平台上用Javascript编写了一个HTML包装器。我将代码打包为Firefox浏览器的两个扩展。其中一个称为MetaStudio,是一个数据模式定义工具,可以对网页进行语义注释。另一种称为DataScraper,是一种从网页中提取数据片段并将其格式化为XML文件的工具
所有源代码都是可读的。请转到下载它们。使用mozilla解析引擎对于您所做的工作来说是非常重要的。只是观察。是的,这看起来是个不错的选择。我觉得Mozilla有点太过分了