JAVA分析html算法(JAVA网页蜘蛛算法)
生活随笔
收集整理的這篇文章主要介紹了
JAVA分析html算法(JAVA网页蜘蛛算法)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
?? 近來有些朋友在做蜘蛛算法,或者在網(wǎng)頁上面做深度的數(shù)據(jù)挖掘。但是遇到復(fù)雜而繁瑣的html頁面大家都望而卻步。因?yàn)楹茈y獲取到相應(yīng)的數(shù)據(jù)。
?? 最古老的辦法的是嘗試用正則表達(dá)式,估計(jì)那么繁瑣的東西得不償失,浪費(fèi)我們寶貴的時(shí)間。
?? 第二個(gè)辦法用開源組織htmlparser的包,這個(gè)是一個(gè)比較老的項(xiàng)目,但是效果估計(jì)不是很好,好像不可以深入分析html,只能分析5級的結(jié)構(gòu);
?? 我這里有個(gè)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;/**** @author Arjick@163.com*/ public class GetLinkTest {public static void main(String[] args) {try {// 通過過濾器過濾出<A>標(biāo)簽Parser parser = new Parser("http://www.lovezan.com");NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {// 實(shí)現(xiàn)該方法,用以過濾標(biāo)簽public boolean accept(Node node) {if (node instanceof LinkTag)// 標(biāo)記 {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.zuzwn.com")) {System.out.println(n.extractLink());}} catch (Exception e) {}}} catch (Exception e) {e.printStackTrace();}} }第三個(gè)辦法,也是我現(xiàn)在一直在用的辦法,首先把html清理為xml,然后用java解析xml獲取數(shù)據(jù),現(xiàn)在上傳一個(gè)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();}} }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zuzwn/p/3602386.html
總結(jié)
以上是生活随笔為你收集整理的JAVA分析html算法(JAVA网页蜘蛛算法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 当clear line vty 命令不起
- 下一篇: web漏洞总结