用正则表达式提取网页中的链接

代码如下:

 1      /** */ /** The regex for search link with the tag "a" */
 2      private  final String A_REGEX = "<a.*?/a>";
 3      /** */ /** The regex for search url with the tag "href" */
 4      private  final String HREF_REGEX = "href=\".*?\"";
 5      /** */ /** The pattern for linke with the tag "a" */
 6      private  final Pattern A_PATTERN = Pattern.compile(A_REGEX);
 7      /** */ /** The pattern for url with the tag "href" */
 8      private  final Pattern HREF_PATTERN = Pattern.compile(HREF_REGEX);
 9      /** */ /**
10     * Get url address from the url and the content of the url
11     * @param url the url need to be get links
12     * @param content the content of the given url
13     * @return a list with the url address of the links
14     */

15      public List<String> getLinkList( URL url, String content )
16      {
17        List<String> linkList = new LinkedList<String>();
18        final Matcher a_matcher = A_PATTERN.matcher(content);
19        while (a_matcher.find()) 
20        {
21            //JUST FOR TEST!
22//            System.out.println(a_matcher.group());
23            //get url address
24            final Matcher myurl = HREF_PATTERN.matcher(a_matcher.group());
25            while (myurl.find())
26            {
27                String urlAddress = myurl.group().replaceAll("href=|>|\"|\"", "");
28                if( urlAddress.startsWith("http") )
29                {
30                    linkList.add(urlAddress);
31                }

32                else if( urlAddress.startsWith("/") || urlAddress.startsWith("\\") )
33                {
34                    linkList.add(url.getPath()+urlAddress);
35                }

36                else
37                {
38                    String fullUrl = url.toString();
39                    //the length of the url without the current page
40                    int lastSlash = fullUrl.lastIndexOf("/") + 1;
41                    linkList.add(fullUrl.substring(0,lastSlash) + urlAddress);
42                }

43            }

44        }

45        return linkList;
46    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值