用htmlparser提取网页网页总是出现错误(在eclipse下运行的)代码如下

hebeijpp 发布于 2011/08/22 09:40
阅读 1K+
收藏 0

package rex.simple;

import org.htmlparser.*;
import org.htmlparser.filters.*;
import org.htmlparser.util.*;
import org.htmlparser.visitors.*; 

public class AstroHTMLParserExtractorTest {
public static void main(String[] args){
 String title;
 String constellation;
 String body;
 String summary;
 try{
 Parser parser=new Parser("http://astro.tom.com/sagittarius.html");
 parser.setEncoding("gb2312");
 NodeFilter filter_constellation_summary=new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","mcol"));
 NodeFilter filter_title=new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","astro_title"));
 NodeFilter filter_body=new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class","asmaincnt"));
 NodeList nodelist=parser.parse(filter_constellation_summary);
 Node node_constellation=nodelist.elementAt(0);
 constellation=node_constellation.toHtml();
 System.out.println("[WHOLE ASTRO:]\n"+constellation);
 parser.reset();
 nodelist=parser.parse(filter_title);
 Node node_title=nodelist.elementAt(0);
 title=node_title.toPlainTextString();
 System.out.println("[TITLE ASTRO:]\n"+title);
 
 nodelist=parser.parse(filter_body);
 Node node_body=nodelist.elementAt(0); 
 Parser body_parser=new Parser(node_body.toHtml());
 TextExtractingVisitor visitor=new TextExtractingVisitor();
 body_parser.visitAllNodesWith(visitor);
 body=visitor.getExtractedText();
 System.out.println("\n\n\n");

 System.out.println("[---TITLE ASTRO---]\n"+title.trim());
 body=body.replaceAll("\t"," ");


 System.out.println("[---body ASTRO---]\n"+body.trim());
 }catch(Exception e){
 e.printStackTrace();
 }
 }
}

加载中
0
鉴客
鉴客
错误是发生在哪一行呢?你的错误中的代码行数和你提供的代码不吻合
0
hebeijpp
hebeijpp

这上面的第32行

鉴客
鉴客
那错误肯定是 node_body 为 null 导致的
0
hebeijpp
hebeijpp

高手呀,终于改好了。谢谢!谢谢!谢谢!谢谢!谢谢!

返回顶部
顶部