htmlParser解析网页链接问题

来源:互联网 发布:windows重命名快捷键 编辑:程序博客网 时间:2024/05/18 17:02

本人在使用htmlparser去分析网站的时候,发现有好多网页不能正确提取其中的链接,不知道怎么回事啊。求指导~~谢谢。

package com.susheng.MoneyMaker.DataExtract;import org.htmlparser.Node;import org.htmlparser.NodeFilter;import org.htmlparser.Parser;import org.htmlparser.tags.LinkTag;import org.htmlparser.util.NodeList;import com.susheng.MoneyMaker.Util.WebEncoding;public class LinkExtract{static String LinkURL = "";public static String getLink(String strURL) throws Exception{WebEncoding web = new WebEncoding();String Character = web.getCharset(strURL);Parser parser = new Parser(strURL);parser.setEncoding(Character);NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter(){// 实现该方法,用以过滤标签public boolean accept(Node node){if (node instanceof LinkTag)// 标记return true;return false;}});// 打印if (nodeList.size() == 0){LinkURL = null;System.out.println("页面不存在链接");} elsefor (int i = 0; i < nodeList.size(); i++){String TextTemp;LinkTag n = (LinkTag) nodeList.elementAt(i);TextTemp = n.getStringText();System.out.println(TextTemp);// if(TextTemp.contains(""))if (TextTemp.contains("联系我们")){System.out.print(TextTemp + " ==>> ");LinkURL = n.extractLink();} else if (TextTemp.contains("联系方式")){System.out.print(TextTemp + " ==>> ");LinkURL = n.extractLink();}else if(TextTemp.contains("联系")){System.out.print(TextTemp + " ==>> ");LinkURL = n.extractLink();}}return LinkURL;}public static void main(String[] args) throws Exception{System.out.println(getLink("http://www.4006601002.com/"));}}


程序源代码如上。运行结果是只有一个链接。页面中明明存在联系方式,但是却找不到。求原因~不胜感激。

 

原创粉丝点击