我正在编写一个应用程序,它接收页面的 HTML 代码并提取页面的某些元素(例如表格)并返回这些元素的 html 代码。我尝试使用 Mozilla 解析器在 java 中执行此操作,以简化页面导航,但在提取所需的 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 解析器在这里似乎有点矫枉过正,我用过 Jericho就您正在做的事情而言,取得了一些成功。
关于java - 用于屏幕抓取的 Mozilla 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1494668/