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


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


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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产伦精品一区二区三区免费迷| 精品国产免费人成电影在线观看 | 在线看片免费人成视频播| 免费h成人黄漫画嘿咻破解版| 亚洲成a人无码亚洲成www牛牛 | 国产精品69白浆在线观看免费| 久久久无码精品亚洲日韩京东传媒| 男人进去女人爽免费视频国产| 日本亚洲成高清一区二区三区 | 亚洲AV无码一区二区三区网址| 好爽好紧好大的免费视频国产| 色吊丝免费观看网站| 亚洲精品和日本精品| 97在线免费视频| 香蕉蕉亚亚洲aav综合| 24小时免费看片| 亚洲乱亚洲乱妇无码| 免费观看日本污污ww网站一区| 日韩久久无码免费毛片软件| 亚洲日韩乱码中文无码蜜桃臀网站 | 亚洲色欲色欲www在线丝| 今天免费中文字幕视频| 亚洲人妖女同在线播放| 四虎影院永久免费观看| 三年片免费高清版 | 特黄特色大片免费| 中文字幕日韩亚洲| 最近免费中文字幕高清大全| 亚洲中文字幕精品久久| 免费大黄网站在线观| 久久成人免费大片| 亚洲最大av资源站无码av网址| 亚洲国产精品狼友中文久久久| a级午夜毛片免费一区二区| 亚洲一级毛片中文字幕| 一本色道久久88亚洲综合| 男人的天堂网免费网站| 性色av极品无码专区亚洲| 亚洲国产精品无码久久久秋霞2| 久久久久av无码免费网| 猫咪免费人成在线网站|