java分析html算法(java网页蜘蛛算法示例)

遇到复杂而繁琐的html页面大家都望而却步。因为很难获取到相应的数据。

最古老的办法的是尝试用正则表达式,估计那么繁琐的东西得不偿失,浪费我们宝贵的时间。

第二个办法用开源组织htmlparser的包,这个是一个比较老的项目,但是效果估计不是很好,好像不可以深入分析html,只能分析5级的结构;

我这里有个htmlparser的源代码,可以获取所有的超链接的

复制代码 代码如下:

   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.util.HashMap;
import java.util.Map;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;


public class GetLinkTest {

    public static void main(String[] args) {

        try {
            // 通过过滤器过滤出标签
            Parser parser = new Parser("//www.jb51.net");
            NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
                // 实现该方法,用以过滤标签
                public boolean accept(Node node) {
                    if (node instanceof LinkTag)// 标记
                    {
                        return true;
                    }
                    return false;
                }
            });
            // 打印
            for (int i = 0; i < nodeList.size(); i++) {
                LinkTag n = (LinkTag) nodeList.elementAt(i);
                //System.out.print(n.getStringText() + " ==>> ");
                //System.out.println(n.extractLink());
                try {
                    if (n.extractLink().equals("//www.jb51.net")) {
                        System.out.println(n.extractLink());
                    }
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

第三个办法,也是我现在一直在用的办法,首先把html清理为xml,然后用java解析xml获取数据,现在上传一个java clean html的源代码:

复制代码 代码如下:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exec;

import java.io.File;
import java.io.IOException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;

/**
 *
 */
public class HtmlClean {

    public void cleanHtml(String htmlurl, String xmlurl) {
        try {
            long start = System.currentTimeMillis();

            HtmlCleaner cleaner = new HtmlCleaner();
            CleanerProperties props = cleaner.getProperties();
            props.setUseCdataForScriptAndStyle(true);
            props.setRecognizeUnicodeChars(true);
            props.setUseEmptyElementTags(true);
            props.setAdvancedXmlEscape(true);
            props.setTranslateSpecialEntities(true);
            props.setBooleanAttributeValues("empty");

            TagNode node = cleaner.clean(new File(htmlurl));

            System.out.println("vreme:" + (System.currentTimeMillis() - start));

            new PrettyXmlSerializer(props).writeXmlToFile(node, xmlurl);

            System.out.println("vreme:" + (System.currentTimeMillis() - start));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java分析html算法(java网页蜘蛛算法示例))