<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 甜菜侯爵 閱讀(450) 評論(0)  編輯  收藏


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


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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲视频手机在线| 亚洲中文字幕无码永久在线| 亚洲国产av一区二区三区丶| 国产一区二区免费视频| 久久久久久a亚洲欧洲aⅴ| 99精品全国免费观看视频..| 国产亚洲美日韩AV中文字幕无码成人| 无码亚洲成a人在线观看| 日日AV拍夜夜添久久免费| 亚洲A∨精品一区二区三区下载| 在线观看免费污视频| 国产亚洲精品AAAA片APP| 免费在线观看毛片| 免费人成大片在线观看播放| 亚洲人成无码网站久久99热国产| 人人鲁免费播放视频人人香蕉| 中国亚洲女人69内射少妇| 嫩草影院在线播放www免费观看| 久久噜噜噜久久亚洲va久| 999在线视频精品免费播放观看| 亚洲午夜久久久精品电影院| 性感美女视频免费网站午夜 | 亚洲av无码一区二区三区不卡| 免费人成网站在线观看不卡| 久久久久亚洲AV无码观看| 男人的好免费观看在线视频| 日韩色视频一区二区三区亚洲| 亚洲国产精品成人一区| 女人隐私秘视频黄www免费| 亚洲视频欧洲视频| 国产午夜免费福利红片| 日韩精品免费视频| 亚洲国产高清国产拍精品| 久久亚洲AV无码西西人体| 国产91免费在线观看| 日本高清不卡中文字幕免费| 久久久久久亚洲av成人无码国产| 成人免费无码视频在线网站| 一本一道dvd在线观看免费视频| 久久久久亚洲精品天堂| 亚洲VA综合VA国产产VA中|