阅读新闻

C#获取远程网页中的所有链接URL(网络蜘蛛实现原理)

[日期:2007-10-28] 来源:  作者: [字体: ]

本文介绍网络蜘蛛获取网页中所有链接的方法,实现原理:使用System.Net.webClient类获取远程网页内容,然后使用URL正则表达式分析Html代码中的链接。代码如下:

using System;

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

namespace HttpGet
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Net.webClient client = new webClient();
byte[] page = client.DownloadData("http://news.163.com");
string content = System.Text.Encoding.UTF8.GetString(page);
string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
Regex re = new Regex(regex);
MatchCollection matches = re.Matches(content);

System.Collections.IEnumerator enu = matches.GetEnumerator();
while (enu.MoveNext() && enu.Current != null)
{
Match match = (Match)(enu.Current);
Console.Write(match.Value + "\r\n");
}
}
}
}



阅读:
录入:admin

评论 】 【 推荐 】 【 打印
上一篇:如何用C#语言构造蜘蛛程序(3)
下一篇:解读C#中的正则表达式大全
相关新闻      
本文评论       全部评论
  地方   (发生 ,昨 09:47 )
发表评论


点评: 字数
姓名:

  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款