java分析html演算法(java網頁蜘蛛演算法樣本)_java

來源:互聯網
上載者:User

遇到複雜而繁瑣的html頁面大家都望而卻步。因為很難擷取到相應的資料。

最古老的辦法的是嘗試用Regex,估計那麼繁瑣的東西得不償失,浪費我們寶貴的時間。

第二個辦法用開源組織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("http://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("http://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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.