当前位置: 移动技术网 > IT编程>开发语言>Java > java分析html算法(java网页蜘蛛算法示例)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

遇到复杂而繁琐的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 {
            // 通过过滤器过滤出<a>标签
            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();
        }
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网