没有合适的资源?快使用搜索试试~ 我知道了~
C#获取远程网页中的所有链接URL C#获取远程网页中的所有链接URL
资源推荐
资源详情
资源评论
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");
[日期: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");
资源评论
juicewall
- 粉丝: 21
- 资源: 228
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功