我现在想把一个html页面中的有用信息提取出来
在网上找了一些资料
都是用下面代码实现的:
public String extractText(String inputHtml) throws Exception
{
StringBuffer text = new StringBuffer();
Parser parser = Parser.createParser(new String(inputHtml.getBytes(),"8859_1"), "8859-1");
// new NodeFilter() {public boolean accept(Node node) {return true;}};
NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {public boolean accept(Node node) {return true;}});
Node node = nodes.elementAt(0);
text.append(new String(node.toPlainTextString().getBytes("8859_1")));
System.out.println(text.toString());
return text.toString();

}
可是我运行此代码后却没有任何结果,也没有错误信息提示。但是在new NodeFilter下有黄色的浪线,提示无用信息。请问这是为什么?
谢谢了

解决方案 »

  1.   

    有个例子,楼主看看
    package test;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;import org.htmlparser.Node;
    import org.htmlparser.NodeFilter;
    import org.htmlparser.Parser;
    import org.htmlparser.filters.NodeClassFilter;
    import org.htmlparser.filters.OrFilter;
    import org.htmlparser.nodes.TextNode;
    import org.htmlparser.parserapplications.StringExtractor;
    import org.htmlparser.tags.LinkTag;
    import org.htmlparser.util.NodeList;
    import org.htmlparser.util.ParserException;
    import org.htmlparser.visitors.HtmlPage;
    import org.htmlparser.visitors.TextExtractingVisitor;
    /**
     * 演示了Html Parse的应用.
     */public class ParseHtmlTest
    {    public static void main(String[] args) throws Exception
        {
            String aFile = "D:\\Eclipse\\workspace\\search\\test001\\content_1349887.htm";        String content = readTextFile(aFile, "GBK");
            StringExtractor se;
            se = new StringExtractor (aFile);
            System.out.println(se.extractStrings(false));        test1(content);
            System.out.println("=====Test1==============================");        test2(content);
            System.out.println("=====Test2==========================");        test3(content);
            System.out.println("=====Test3===============================");        test4(content);
            System.out.println("=====Test4===============================");        test5(aFile);
           System.out.println("======Test5==============================");        //访问外部资源,相对慢
            test5("http://www.medlink.com.cn"); 
            System.out.println("====================================");    }    /**
         * 读取文件的方式来分析内容.
         * filePath也可以是一个Url.
         * 
         * @param resource 文件/Url
         */
        public static void test5(String resource) throws Exception
        {
            Parser myParser = new Parser(resource);        //设置编码
            myParser.setEncoding("GBK");        HtmlPage visitor = new HtmlPage(myParser);        myParser.visitAllNodesWith(visitor);        String textInPage = visitor.getBody().toString();        System.out.println(textInPage);
        }    /**
         * 按页面方式处理.对一个标准的Html页面,推荐使用此种方式.
         */
        public static void test4(String content) throws Exception
        {
            Parser myParser;
            myParser = Parser.createParser(content, "GBK");        HtmlPage visitor = new HtmlPage(myParser);        myParser.visitAllNodesWith(visitor);        String textInPage = visitor.getTitle();        System.out.println(textInPage);
            System.out.println("--------------------");
            System.out.println(visitor.getBody());
        }    /**
         * 利用Visitor模式解析html页面.
         *
         * 小优点:翻译了<>等符号 
         * 缺点:好多空格,无法提取link
         *   
         */
        public static void test3(String content) throws Exception
        {
            Parser myParser;
            myParser = Parser.createParser(content, "GBK");        TextExtractingVisitor visitor = new TextExtractingVisitor();        myParser.visitAllNodesWith(visitor);        String textInPage = visitor.getExtractedText();        System.out.println(textInPage);
        }    /**
         * 得到普通文本和链接的内容.
         * 
         * 使用了过滤条件.
         */
        public static void test2(String content) throws ParserException
        {
            Parser myParser;
            NodeList nodeList = null;        myParser = Parser.createParser(content, "GBK");        NodeFilter textFilter = new NodeClassFilter(TextNode.class);
            NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);        //暂时不处理 meta
            //NodeFilter metaFilter = new NodeClassFilter(MetaTag.class);        OrFilter lastFilter = new OrFilter();
            lastFilter.setPredicates(new NodeFilter[] { textFilter, linkFilter });        nodeList = myParser.parse(lastFilter);        Node[] nodes = nodeList.toNodeArray();        for (int i = 0; i < nodes.length; i++)
            {
                Node anode = (Node) nodes[i];            String line = "";
                if (anode instanceof TextNode)
                {
                    TextNode textnode = (TextNode) anode;
                    line = textnode.toPlainTextString().trim();
                    //line = textnode.getText();
                }
    //            else if (anode instanceof LinkTag)
    //            {
    //                LinkTag linknode = (LinkTag) anode;
    //
    //                line = linknode.getLink();
    //                //@todo 过滤jsp标签:可以自己实现这个函数
    //                //line = StringFunc.replace(line, "<%.*%>", "");
    //            }            if (isTrimEmpty(line))
                    continue;            System.out.println(line);
            }
        }    /**
         * 解析普通文本节点.
         * 
         * @param content
         * @throws ParserException
         */
        public static void test1(String content) throws ParserException
        {
            Parser myParser;
            Node[] nodes = null;        myParser = Parser.createParser(content, null);        nodes = myParser.extractAllNodesThatAre(TextNode.class); //exception could be thrown here        for (int i = 0; i < nodes.length; i++)
            {
                TextNode textnode = (TextNode) nodes[i];
                String line = textnode.toPlainTextString().trim();
                if (line.equals(""))
                    continue;
                System.out.println(line);
            }    }    /**
         * 读取一个文件到字符串里.
         * 
         * @param sFileName  文件名
         * @param sEncode   String
         * @return 文件内容
         */
        public static String readTextFile(String sFileName, String sEncode)
        {
            StringBuffer sbStr = new StringBuffer();        try
            {
                File ff = new File(sFileName);
                InputStreamReader read = new InputStreamReader(new FileInputStream(ff),
                        sEncode);
                BufferedReader ins = new BufferedReader(read);            String dataLine = "";
                while (null != (dataLine = ins.readLine()))
                {
                    sbStr.append(dataLine);
                    sbStr.append("\r\n");
                }            ins.close();
            }
            catch (Exception e)
            {
               // LogMan.error("read Text File Error", e);
            }        return sbStr.toString();
        }    /**
         * 去掉左右空格后字符串是否为空
         * @param astr String
         * @return boolean
         */
        public static boolean isTrimEmpty(String astr)
        {
            if ((null == astr) || (astr.length() == 0))
            {
                return true;
            }
            if (isBlank(astr.trim()))
            {
                return true;
            }
            return false;
        }    /**
         * 字符串是否为空:null或者长度为0.
         * @param astr 源字符串.
         * @return boolean
         */
        public static boolean isBlank(String astr)
        {
            if ((null == astr) || (astr.length() == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }}
      

  2.   

    我用下面的代码
    public static void main(String[] args) throws Exception 
    {
    Parser parser = new Parser("http://2008.sohu.com/20060210/n241768433.shtml");
    TextExtractingVisitor visitor = new TextExtractingVisitor();
    parser.visitAllNodesWith(visitor);
    System.out.println(visitor.getExtractedText().trim());
    }
    可是运行结果中有许多空格,并且还有下面的内容出现 请问为什么?
    td,input,select {font-size:12px;color:#000000;}.ui_top{color:#000000;font-size:12px;line-height:18px;}
    .ui_top a:link{color:#000000;text-decoration:none;}
    .ui_top a:visited{color:#000000;text-decoration:none;}
    .ui_top a:hover {color:#000000;text-decoration:underline;}
    a.link,a.:visited {color: #0071AE;text-decoration:none;line-height:18px;}
    a.:hover{text-decoration:underline;}
    var clickCount = 0;
    function clearCommentContent(oObject) {
    clickCount++;
    if (clickCount == 1) {
    oObject.value = "";
    }
    }
    function checkCommentContent(oObject) {
    if (clickCount == 0)
    alert("请先填写您的个人看法");
    else if(checkInput() != false)
    oObject.form.submit();
    }
      

  3.   

    但页面不是GBK而是用utf-8编码的呢?