惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
U
Unit 42
F
Fortinet All Blogs
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
Y
Y Combinator Blog
罗磊的独立博客
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
I
InfoQ
博客园 - 叶小钗
博客园 - 聂微东
Last Week in AI
Last Week in AI

博客园 - hello csharp

[导入]Nhibernate引入自定义Membership和Role 推荐个不错的屏幕捕获程序 测试Google Docs 发布的文章.。。 Guidelines – a hidden feature for the Visual Studio Editor Gridview, ObjectDataSource Making life easy Applied MS Reporting Services 101 using Smart Client Database Provider-based ASP.NET Membership Provider HTMLEditor Provider - How to write a custom provider for ASP.NET 2.0 Firebug-javascript/css/ajax/dom调试器【这款不错】 Automatic Sql Server Backup Utility Using sqlserveragent LiveWriter测试 发布个小软件给大伙玩玩 [js小技巧]鼠标移到图片高亮度显示 dot net html分析类库 系统发邮件测试 Dumbster 读新浪博客示例[源代码下载] Intellisense for the NHibernate XML Schemas Develop Reports Using Crystal Reports in .NET 2005 [小技巧]winfrom使用多线程
【小技巧】如何得到一个网页的所有a标记 herf 链接代码
hello csharp · 2006-10-26 · via 博客园 - hello csharp


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

1.先取得网页的原代码

 Uri url=new Uri("http://www.blogjava.net/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.blogjava.net/wujun/CommentsRSS.aspx
http://www.blogjava.net/wujun/archive/2006/10/23/47150.html#76745
http://www.blogjava.net/wujun/archive/2006/10/23.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html
http://www.blogjava.net/wujun/archive/2006/10/23/76769.html#FeedBack
http://www.blogjava.net/wujun/admin/EditPosts.aspx?postid=76769
http://www.blogjava.net/wujun/AddToFavorite.aspx?id=76769
http://www.blogjava.net/wujun/archive/2006/10/20.html
 ......
..............
 .........................等等等。。。