加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 01:30:15 所属栏目:百科 来源:网络整理
导读:代码如下: 1 /***/ /** Theregexforsearchlinkwiththetag"a" */ 2 private final StringA_REGEX="a.*?/a"; 3 /***/ /** Theregexforsearchurlwiththetag"href" */ 4 private final StringHREF_REGEX="href=".*?""; 5 /***/ /** Thepatternforlinkewiththe

代码如下:

1 /***/ /**Theregexforsearchlinkwiththetag"a"*/
2 private finalStringA_REGEX="<a.*?/a>";
3 /***/ /**Theregexforsearchurlwiththetag"href"*/
4 private finalStringHREF_REGEX="href=".*?"";
5 /***/ /**Thepatternforlinkewiththetag"a"*/
6 private finalPatternA_PATTERN=Pattern.compile(A_REGEX);
7 /***/ /**Thepatternforurlwiththetag"href"*/
8 private finalPatternHREF_PATTERN=Pattern.compile(HREF_REGEX);
9 /***/ /**
10*Geturladdressfromtheurlandthecontentoftheurl
11*@paramurltheurlneedtobegetlinks
12*@paramcontentthecontentofthegivenurl
13*@returnalistwiththeurladdressofthelinks
14*/

15 publicList<String>getLinkList(URLurl,Stringcontent)
16 {
17List<String>linkList=newLinkedList<String>();
18finalMatchera_matcher=A_PATTERN.matcher(content);
19while(a_matcher.find())
20{
21//JUSTFORTEST!
22//System.out.println(a_matcher.group());
23//geturladdress
24finalMatchermyurl=HREF_PATTERN.matcher(a_matcher.group());
25while(myurl.find())
26{
27StringurlAddress=myurl.group().replaceAll("href=|>|"|"","");
28if(urlAddress.startsWith("http"))
29{
30linkList.add(urlAddress);
31}

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

36else
37{
38StringfullUrl=url.toString();
39//thelengthoftheurlwithoutthecurrentpage
40intlastSlash=fullUrl.lastIndexOf("/")+1;
41linkList.add(fullUrl.substring(0,lastSlash)+urlAddress);
42}

43}

44}

45returnlinkList;
46}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读