蜘蛛爬虫
来源:互联网 发布:人工智能主要应用领域 编辑:程序博客网 时间:2024/03/29 20:26
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//需要解析的集合
List<string> list = new List<string>();
//已经解析的集合
List<string> listCount = new List<string>();
list.Add("http://www.baidu.com");
ReadHtml(list,listCount);
Console.ReadLine();
}
/// <summary>
/// 读取HTML中的URL
/// </summary>
/// <param name="list"></param>
/// <param name="listCount"></param>
public static void ReadHtml(List<string> list, List<string> listCount)
{
List<string> count = new List<string>();
for (int a = 0; a < list.Count; a++)
{
//没有解析过该项
if (!listCount.Contains(list[a]))
{
try
{
//在已解析过的集合里面添加本条数据
listCount.Add(list[a]);
WebRequest req = WebRequest.Create(list[a]);
WebResponse result = req.GetResponse();
//得到的流是网页内容
Stream ReceiveStream = result.GetResponseStream();
StreamReader readerOfStream = new StreamReader(ReceiveStream, System.Text.Encoding.GetEncoding("GB2312"));
//得到当前URL的源码
string str = readerOfStream.ReadToEnd();
//解析
Regex regex = new Regex(@"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
foreach (Match mc in regex.Matches(str))
{
Regex regexOhter = new Regex(list[a] + "|.png|.jpg|.gif|.bmp|.js|.css|.xls|.doc|.pdf|.chw|.exe|.mp3|.mp4|.avi|.swf|.xml");
if (!regexOhter.IsMatch(mc.ToString()))
{
Console.WriteLine(mc);
count.Add(mc.ToString());
}
}
Console.WriteLine("----------------------解析完一个页面!--------------------");
if (a == list.Count - 1)
{
//递归调用本方法
ReadHtml(count, listCount);
}
}
catch (System.Exception ex) { }
finally
{
List<string> error = new List<string>();
//如果出错在出错的后面一条URL继续解析
for (int z = a + 1; z < list.Count; z++)
{
error.Add(list[z]);
}
//继续解析
ReadHtml(error, listCount);
}
}
}
}
}
}
- 蜘蛛爬虫
- 网络蜘蛛爬虫原理
- 搜索引擎蜘蛛爬虫原理
- 搜索引擎蜘蛛爬虫原理
- 蜘蛛网络爬虫多线程
- Web Spider 网络蜘蛛爬虫
- 禁止搜索引擎蜘蛛(爬虫)
- C#构造蜘蛛爬虫程序
- Nginx防蜘蛛爬虫处理
- Nginx防蜘蛛爬虫处理
- Nginx防蜘蛛爬虫处理
- nginx预防蜘蛛爬虫处理
- Nginx防蜘蛛爬虫处理
- 第三章 爬虫和蜘蛛
- 爬虫/蜘蛛程序的制作C#语言
- 淘宝网开始屏蔽百度蜘蛛爬虫
- 构造C#语言的爬虫蜘蛛程序
- C#语言实现网站爬虫蜘蛛程序
- JeeSite 默认MySql,让我们来看看如何更换为Oracle数据库
- 隐形吉他Air Guitar Move
- fork() 和 vfork 的区别?
- python下连接mysql的错误 错误码 (10061)
- c语言:位运算符
- 蜘蛛爬虫
- Algorithm Practice for 1575
- firefox浏览器优化-速度超chrome
- 系统性能优化 Advanced SystemCare pro v6.1.9.215 多国语言特别版
- Hibernate中两种获取Session的方式 (转)
- C++函数对象与函数指针不同之处
- C语言编译过程总结详解
- memcached 完全解剖3——分布式算法
- C语言的那些秘密之---函数返回局部变量