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 閱讀(1845) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 大宁县| 剑河县| 濮阳县| 金乡县| 全州县| 仪征市| 罗江县| 云林县| 厦门市| 大关县| 蓬溪县| 大足县| 黄冈市| 崇明县| 丰城市| 鄂尔多斯市| 渭源县| 赫章县| 资溪县| 汽车| 白玉县| 固镇县| 大英县| 东光县| 广安市| 瑞安市| 灵台县| 固镇县| 三亚市| 大庆市| 凤山县| 轮台县| 习水县| 思南县| 喀喇| 潮州市| 台南县| 孟连| 拜泉县| 鄂尔多斯市| 蒙自县|