<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    用正則表達式提取網頁中的鏈接

    個人感覺效率肯定還能進一步提高。。。。
    不過實在是對正則不是太熟悉,只好暫時這樣了。

    代碼如下:

     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    }

    posted on 2009-11-05 03:00 甜菜侯爵 閱讀(458) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2009年11月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲性色AV日韩在线观看| 日韩中文字幕免费| 亚洲AV天天做在线观看| 日韩精品无码永久免费网站| 四虎影院免费在线播放| 亚洲精品国产摄像头| 日韩高清免费在线观看| 久久亚洲精品无码gv| 四虎免费久久影院| av午夜福利一片免费看久久| 国产综合亚洲专区在线| 在线免费播放一级毛片| 亚洲AV人人澡人人爽人人夜夜| 一级毛片免费不卡在线| 亚洲国产成人久久| 免费高清在线影片一区| 美女扒开屁股让男人桶爽免费| 亚洲AV蜜桃永久无码精品| 黄床大片免费30分钟国产精品| 亚洲综合精品香蕉久久网| 一级做a爰全过程免费视频| 亚洲明星合成图综合区在线| 成年性午夜免费视频网站不卡| 亚洲成av人在线观看网站| 亚洲国产黄在线观看| 久久久精品免费视频| 亚洲成人福利在线| 爽爽日本在线视频免费| 久久精品成人免费观看97| 亚洲狠狠综合久久| 在线jlzzjlzz免费播放| 一级特黄录像视频免费| 久久丫精品国产亚洲av不卡 | 岛国精品一区免费视频在线观看| 在线涩涩免费观看国产精品 | 中国亚洲呦女专区| 亚洲高清无码综合性爱视频| 最近免费字幕中文大全| 2020天堂在线亚洲精品专区| 少妇亚洲免费精品| 国产高清免费视频|