html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

C#获取远程网页中的所有链接URL(网络蜘蛛实现原理)_.NET教程_编程技术

[ ] 已经帮助:人解决问题
链接|网络|网页

本文介绍网络蜘蛛获取网页中所有链接的方法,实现原理:使用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");
}
}
}
}

J75HTML5中文学习网 - HTML5先行者学习网
J75HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助