<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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 24小时免费直播在线观看| 一级做a免费视频观看网站| 91福利免费体验区观看区| 国产精一品亚洲二区在线播放| 免费人成大片在线观看播放| 国产男女猛烈无遮挡免费网站| 国产亚洲综合久久| 免费人成网站7777视频| 黄网站在线播放视频免费观看| 四虎免费永久在线播放| 免费看美女午夜大片| 亚洲综合AV在线在线播放| APP在线免费观看视频| 亚洲人成电影福利在线播放| 182tv免费观看在线视频| 亚洲H在线播放在线观看H| 精品免费国产一区二区| 天堂亚洲免费视频| 亚洲成色WWW久久网站| 7723日本高清完整版免费| 亚洲另类自拍丝袜第五页| 亚洲综合激情另类专区| 日韩内射激情视频在线播放免费 | 亚洲免费在线观看| 亚洲AV无码一区东京热久久 | 亚洲一区二区三区四区视频| 日韩精品免费一区二区三区| 国产免费牲交视频免费播放| 亚洲精品国产肉丝袜久久| 日韩在线免费看网站| 99久久成人国产精品免费| 亚洲a级成人片在线观看| 亚洲国产精品国产自在在线| 亚洲精品免费在线观看| 亚洲国产精品精华液| 亚洲精品卡2卡3卡4卡5卡区| 一个人免费观看在线视频www | 污污污视频在线免费观看| 亚洲精品高清国产麻豆专区| 国产99视频免费精品是看6| 午夜精品免费在线观看|