zoukankan      html  css  js  c++  java
  • 使用HtmlParser提取网页中的链接

    关于HtmpParser的基本内容请见 HtmlParser基础教程

    本文示例用于提取HTML文件中的链接

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package org.ljh.search.html;  
    2.   
    3. import java.util.HashSet;  
    4. import java.util.Set;  
    5.   
    6. import org.htmlparser.Node;  
    7. import org.htmlparser.NodeFilter;  
    8. import org.htmlparser.Parser;  
    9. import org.htmlparser.filters.NodeClassFilter;  
    10. import org.htmlparser.filters.OrFilter;  
    11. import org.htmlparser.tags.LinkTag;  
    12. import org.htmlparser.util.NodeList;  
    13. import org.htmlparser.util.ParserException;  
    14.   
    15. //本类创建用于HTML文件解释工具  
    16. public class HtmlParserTool {  
    17.   
    18.     // 本方法用于提取某个html文档中内嵌的链接  
    19.     public static Set<String> extractLinks(String url, LinkFilter filter) {  
    20.         Set<String> links = new HashSet<String>();  
    21.         try {  
    22.             // 1、构造一个Parser,并设置相关的属性  
    23.             Parser parser = new Parser(url);  
    24.             parser.setEncoding("gb2312");  
    25.   
    26.             // 2.1、自定义一个Filter,用于过滤<Frame >标签,然后取得标签中的src属性值  
    27.             NodeFilter frameNodeFilter = new NodeFilter() {  
    28.                 @Override  
    29.                 public boolean accept(Node node) {  
    30.                     if (node.getText().startsWith("frame src=")) {  
    31.                         return true;  
    32.                     } else {  
    33.                         return false;  
    34.                     }  
    35.                 }  
    36.             };  
    37.               
    38.             //2.2、创建第二个Filter,过滤<a>标签  
    39.             NodeFilter aNodeFilter = new NodeClassFilter(LinkTag.class);  
    40.               
    41.             //2.3、净土上述2个Filter形成一个组合逻辑Filter。  
    42.             OrFilter linkFilter = new OrFilter(frameNodeFilter, aNodeFilter);  
    43.               
    44.             //3、使用parser根据filter来取得所有符合条件的节点  
    45.             NodeList nodeList = parser.extractAllNodesThatMatch(linkFilter);  
    46.               
    47.             //4、对取得的Node进行处理  
    48.             for(int i = 0; i<nodeList.size();i++){  
    49.                 Node node = nodeList.elementAt(i);  
    50.                 String linkURL = "";  
    51.                 //如果链接类型为<a />  
    52.                 if(node instanceof LinkTag){  
    53.                     LinkTag link = (LinkTag)node;  
    54.                     linkURL= link.getLink();  
    55.                 }else{  
    56.                     //如果类型为<frame />  
    57.                     String nodeText = node.getText();  
    58.                     int beginPosition = nodeText.indexOf("src=");  
    59.                     nodeText = nodeText.substring(beginPosition);  
    60.                     int endPosition = nodeText.indexOf(" ");  
    61.                     if(endPosition == -1){  
    62.                         endPosition = nodeText.indexOf(">");  
    63.                     }  
    64.                     linkURL = nodeText.substring(5, endPosition - 1);  
    65.                 }  
    66.                 //判断是否属于本次搜索范围的url  
    67.                 if(filter.accept(linkURL)){  
    68.                     links.add(linkURL);  
    69.                 }  
    70.             }  
    71.               
    72.         } catch (ParserException e) {  
    73.             e.printStackTrace();  
    74.         }  
    75.         return links;  
    76.     }  
    77. }  



    程序中的一些说明:

    (1)通过Node#getText()取得节点的String。

    (2)node instanceof TagLink,即<a/>节点,其它还有很多的类似节点,如tableTag等,基本上每个常见的html标签均会对应一个tag。官方文档说明如下:

    org.htmlparser.nodes The nodes package has the concrete node implementations.
    org.htmlparser.tags The tags package contains specific tags.

    因此可以通过此方法直接判断一个节点是否某个标签内容。

    其中用到的LinkFilter接口定义如下:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package org.ljh.search.html;  
    2.   
    3. //本接口所定义的过滤器,用于判断url是否属于本次搜索范围。  
    4. public interface LinkFilter {  
    5.     public boolean accept(String url);  
    6. }  



    测试程序如下:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package org.ljh.search.html;  
    2.   
    3. import java.util.Iterator;  
    4. import java.util.Set;  
    5.   
    6. import org.junit.Test;  
    7.   
    8. public class HtmlParserToolTest {  
    9.   
    10.     @Test  
    11.     public void testExtractLinks() {  
    12.         String url = "http://www.baidu.com";  
    13.         LinkFilter linkFilter = new LinkFilter(){  
    14.             @Override  
    15.             public boolean accept(String url) {  
    16.                 if(url.contains("baidu")){  
    17.                     return true;  
    18.                 }else{  
    19.                     return false;  
    20.                 }  
    21.             }  
    22.               
    23.         };  
    24.         Set<String> urlSet = HtmlParserTool.extractLinks(url, linkFilter);  
    25.           
    26.         Iterator<String> it = urlSet.iterator();  
    27.         while(it.hasNext()){  
    28.             System.out.println(it.next());  
    29.         }  
    30.     }  
    31.   
    32. }  



    输出结果如下:

    http://www.hao123.com
    http://www.baidu.com/
    http://www.baidu.com/duty/
    http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=
    http://music.baidu.com
    http://ir.baidu.com
    http://www.baidu.com/gaoji/preferences.html
    http://news.baidu.com
    http://map.baidu.com
    http://music.baidu.com/search?fr=ps&key=
    http://image.baidu.com
    http://zhidao.baidu.com
    http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&nc=1&word=
    http://www.baidu.com/more/
    http://shouji.baidu.com/baidusearch/mobisearch.html?ref=pcjg&from=1000139w
    http://wenku.baidu.com
    http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=
    https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
    http://www.baidu.com/cache/sethelp/index.html
    http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt
    http://tieba.baidu.com/f?kw=&fr=wwwt
    http://home.baidu.com
    https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
    http://v.baidu.com
    http://e.baidu.com/?refer=888
    ;
    http://tieba.baidu.com
    http://baike.baidu.com
    http://wenku.baidu.com/search?word=&lm=0&od=0
    http://top.baidu.com
    http://map.baidu.com/m?word=&fr=ps01000

  • 相关阅读:
    如何用Percona XtraBackup进行MySQL从库的单表备份和恢复【转】
    8款实用Sublime text 3插件推荐
    windows下配置nginx+php环境
    Windows10+Ubuntu双系统安装[
    window yii2 安装插件 报yiisoft/yii2 2.0.x-dev requires ext-mbstring错
    Composer常见问题
    Yii2中如何使用CodeCeption
    php 单进程SAPI生命周期
    php的SAPI,CLI SAPI,CGI SAPI
    HTTPS服务器配置
  • 原文地址:https://www.cnblogs.com/waiwai1015/p/4772981.html
Copyright © 2011-2022 走看看