blogjava's web log

          blogjava's web log
          ...

          【小技巧】如何得到一個網頁的所有a標記 herf 鏈接代碼





          using ?System.IO;
          using ?System.Text;
          using ?System.Text.RegularExpressions;
          using ?System.Net;

          1.先取得網頁的原代碼

          ?Uri?url=new?Uri("http://www.aygfsteel.com/wujun");
          ????????????HttpWebRequest?request
          =(HttpWebRequest)?WebRequest.Create(url);
          ????????????HttpWebResponse?response?
          =?(HttpWebResponse)request.GetResponse();
          ????????????Stream?stream?
          =?response.GetResponseStream();
          ????????????StreamReader?sr?
          =?new?StreamReader(stream);
          ????????????
          string?str=sr.ReadToEnd();
          ????????????sr.Close();
          ????????????stream.Close();
          ????????????response.Close();

          得到網頁的html源代碼以后。再根據源代碼分析 所有 <a href ="url">?? 最后得到 href后面? url的鏈接地址

          正則表達式
          ????Regex?RegExFindHref?=?new?Regex(@"<a\s+([^>]*\s*)?href\s*=\s*(?:""(?<1>[/\a-z0-9_][^""]*)""|'(?<1>[/\a-z0-9_][^']*)'
          |(?<1>[/\a-z0-9_]\S*))(\s[^>]*)?>(?<2>.*?)</a>
          ",?RegexOptions.Singleline?|?RegexOptions.IgnoreCase?|?RegexOptions.Compiled);



          循環讀出 連接地址

          ?????for?(Match?m?=?RegExFindHref.Match(str);?m.Success;?m?=?m.NextMatch())
          ????????????{
          ???????????????TextBox1.Text
          +=?m.Groups[1].ToString()+"\n";
          ????????????

          ????????????}

          運行后
          TextBox1 將顯示分析后的所有網頁的連接 :

          http://www.dotlucene.net/
          http://www.castleproject.org/
          http://www.codeplex.com/
          http://www.codeproject.com/
          http://www.asp.net/
          http://www.nhibernate.org/
          http://www.aygfsteel.com/wujun/CommentsRSS.aspx
          http://www.aygfsteel.com/wujun/archive/2006/10/23/47150.html#76745
          http://www.aygfsteel.com/wujun/archive/2006/10/23.html
          http://www.aygfsteel.com/wujun/archive/2006/10/23/76769.html
          http://www.aygfsteel.com/wujun/archive/2006/10/23/76769.html
          http://www.aygfsteel.com/wujun/archive/2006/10/23/76769.html#FeedBack
          http://www.aygfsteel.com/wujun/admin/EditPosts.aspx?postid=76769
          http://www.aygfsteel.com/wujun/AddToFavorite.aspx?id=76769
          http://www.aygfsteel.com/wujun/archive/2006/10/20.html
          ?......
          ..............
          ?.........................等等等。。。



          剛剛看到一個JAVA版。
          地址:? http://www.aygfsteel.com/ekinglong/archive/2006/10/27/77688.html


          posted on 2006-10-26 12:46 record java and net 閱讀(1838) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 武宣县| 仪征市| 工布江达县| 敦化市| 临潭县| 利津县| 玉山县| 麻城市| 延津县| 潜江市| 盐津县| 城口县| 许昌县| 昔阳县| 金秀| 蒲城县| 陆河县| 孙吴县| 平陆县| 凯里市| 鸡泽县| 新田县| 庄河市| 方城县| 仁布县| 民和| 凤阳县| 五峰| 兴和县| 六盘水市| 准格尔旗| 固阳县| 奈曼旗| 马鞍山市| 合川市| 安塞县| 射洪县| 康定县| 婺源县| 五峰| 海口市|