`

HTML parser选型测试

    博客分类:
  • CMS
阅读更多
内容管理(cms)常常需要将网站频道的摘要(summery)合并到父频道的封面,引入HTML parser,
 可以结构化方式操作HTML内容,使网页内容的提取、重构变得容易。
 以下链接列出了相关的java opensource项目
 http://www.open-open.com/30.htm
 根据网友的评论,将htmlcleaner、htmlparser、nekohtml列入候选。
 以附件html作为测试用例,按照常见的getElementsByTagName提取Body,
 以getElementById获取id为'content6'的script
 测试编码如下:
java 代码
 
  1. static public String Neko(String path) throws SAXException, IOException{  
  2.     DOMParser parser = new DOMParser();  
  3.     //InputSource in = new InputSource(new Reader());  
  4.     parser.parse(TPPath+path);  
  5.     Document doc=parser.getDocument();  
  6.     org.w3c.dom.NodeList nl = doc.getElementsByTagName("body");  
  7.     System.out.println(printNode(nl.item(0)));  
  8.     System.out.println("----------------------------------------");  
  9.     org.w3c.dom.Element n = doc.getElementById("content6");  
  10.     System.out.println(printNode(n));  
  11.     return "";  
  12. }  
  13. static public String htmlparser(String path) throws ParserException{  
  14.     // one of several constructors  
  15.     Parser p = new Parser(TPPath+path);  
  16.     NodeList nl=p.parse(new TagNameFilter("body"));  
  17.     System.out.println(nl.elementAt(0).toHtml());  
  18.     System.out.println("----------------------------------------");  
  19.     Parser p2 = new Parser(TPPath+path);  
  20.     NodeList nl2=p2.parse(new HasAttributeFilter("id","content6"));  
  21.     System.out.println(nl2.elementAt(0).toHtml());  
  22.     return "";  
  23. }  
  24. static public String htmlcleaner(String path) throws Exception{  
  25.     // one of several constructors  
  26.     HtmlCleaner cleaner = new HtmlCleaner(new File(TPPath+path));  
  27.     org.w3c.dom.Document doc = cleaner.createDOM();  
  28.     org.w3c.dom.NodeList nl = doc.getElementsByTagName("body");  
  29.     System.out.println(printNode(nl.item(0)));  
  30.     System.out.println("----------------------------------------");  
  31.     org.w3c.dom.Element n=doc.getElementById("content6");  
  32.     System.out.println(printNode(n));  
  33.     return "";  
  34. }  
一个打印dom节点的辅助方法如下:
java 代码
 
  1. public static String printNode(Node node) {  
  2.     StringBuffer sbuf=new StringBuffer();  
  3.     String nn=node.getNodeName();  
  4.     boolean btag=true;  
  5.     if(nn.equals("#text")) btag=false;  
  6.     if(btag){  
  7.       if(node.hasAttributes()){  
  8.       NamedNodeMap attrs=node.getAttributes();  
  9.       StringBuffer abuf= new StringBuffer();  
  10.       for(int i=0,len=attrs.getLength(); i<len; i++){  
  11.           Node attr=attrs.item(i);  
  12.           abuf.append(" "+attr.getNodeName()+"=\""+attr.getNodeValue()+"\"");  
  13.       }  
  14.       sbuf.append("<"+nn+abuf.toString()+">");  
  15.       }else  sbuf.append("<"+nn+">");  
  16.     }  
  17.     if(node.hasChildNodes()){  
  18.      Node child = node.getFirstChild();  
  19.      sbuf.append(child.getNodeValue());  
  20.      while (child != null) {  
  21.          sbuf.append(printNode(child));  
  22.          child = child.getNextSibling();  
  23.      }  
  24.     }  
  25.     if(btag)  
  26.       sbuf.append("</"+nn+">");  
  27.     return sbuf.toString();  
  28. }  

 测试结果如下:
                   getElementsByTagName        getElementById
htmlcleaner         抛出异常java.lang.NoSuchFieldError: fRecognizedFeatures
htmlparser         在分析到script中的字符串包含"</b>"出现逻辑错误,将该</b>误判为script结束
nekohtml            pass                        pass

nekohtml入选。
分享到:
评论
1 楼 chen4w 2007-08-14  
从数据库表中直接获取的数据形式上可能并不满足要求,你看sina新闻的摘要,标题字数都对仗得那么好,没有人工干预恐怕很难做到

相关推荐

    Java网络爬虫(蜘蛛)源码_zhizhu.rar

    在"文档说明.rar"中,可能包含了该项目的详细设计文档,包括爬虫的工作原理、技术选型、架构设计、数据存储方案以及如何运行和测试代码等内容。通常,一个完整的网络爬虫项目会涉及以下几个核心部分: 1. **URL管理...

    python实现下载指定网址所有图片的方法

    #### 二、技术选型 为了实现上述目标,本项目采用了以下技术和库: - **Python**:作为主要的编程语言,其丰富的库支持使得开发变得高效便捷。 - **`urllib`**:用于发送 HTTP 请求和接收响应数据。 - **`...

    爬虫开发大纲资料.txt

    依赖库的安装是指安装编程语言中用于网络请求、数据解析等操作的第三方库,例如Python中的requests用于发送HTTP请求,lxml和html.parser用于解析网页内容。考虑到反爬虫机制的存在,代理设置成为了一个必要的环节,...

    Python四周实现爬虫系统-视频课程资源网盘链接提取码下载 .txt

    soup = BeautifulSoup(response.text, 'html.parser') # 解析页面寻找下载链接 download_url = '...' # 假设已找到具体的下载链接 # 下载文件 file_name = 'Python数据分析实战集训营' file_path = os.path....

    前后端分离系统详细介绍

    该模式强调的是将应用程序的前端(用户界面层)与后端(服务端逻辑层)进行解耦,以便它们能够独立地开发、测试、部署以及维护。这种分离有助于简化复杂系统的构建过程,并提高了团队协作的效率。 #### 二、前后端...

Global site tag (gtag.js) - Google Analytics