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.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 安宁市| 宁化县| 乌海市| 克山县| 呼图壁县| 阿拉善盟| 宁蒗| 哈密市| 雷山县| 图木舒克市| 阿城市| 肇源县| 永康市| 休宁县| 新干县| 理塘县| 张家港市| 东海县| 桂阳县| 读书| 甘洛县| 巢湖市| 西昌市| 张北县| 平远县| 安康市| 姜堰市| 涟源市| 西昌市| 青海省| 湘潭县| 同心县| 朝阳区| 德惠市| 响水县| 屏东市| 姚安县| 汤阴县| 新昌县| 江西省| 漳州市|