关键技术之单机爬虫的实现(1)---我的名字叫小蛛蛛,虽然我还小,但是我也是只可爱的蜘蛛。

        大家好,我的中文名字叫小蛛蛛英文名字叫jspider。现在刚刚从“妈妈”肚子出来。动作比较缓慢,不过对外界东西已经有反应了。别说我坏话哈,我听的懂的。刚出来还不知道我到底来到世界是干嘛的,后来查了下资料。才知道:

        网络爬虫,又称网络蜘蛛,是指某个能以人类无法达到的速度不断重复执行某项任务的自动程序。由于专门用于检索信息的爬虫程序像蜘蛛(Spider)一样在网络间爬来爬去,因此,搜索引擎的Crawler程序被称为Spider程序。世界上第一个Crawler程序,是MIT Matthew Gray的World wide WebWanderer,用于追踪互联网发展规模。刚开始它只用来统计互联网上的服务器数量,后来则发展为也能够捕获网址(URL)。随着互联网的迅速发展,使得检索所有新出现的网页变得越来越困难,因此,在Wanderer基础上,一些编程者将传统的Crawler程序工作原理作了些改进。其设想是,既然所有网页都可能有连向其他网站的链接,那么从一个网站开始,跟踪所有网页上的所有链接,就有可能检索整个互联网。到1993年底,一些基于此原理的搜索引擎开始纷纷涌现,其中最负盛名的三个是:Scotland的JumpStation,Colorado大学Oliver McBryan的The World Wide WebWorm(First Mention of McBryan's World Wide Web Worm),NASA的Repository-Based Software Engineering(RBSE)Crawler。

       发现自己起的作用还挺大,连搜索引擎都离开不我。好羡慕上面的大蛛蛛们哦,竟然能把绝大网页都爬到。没办法谁让别人有个好“妈妈”呢。不管了,还是接受现实吧。看看我“妈妈”都给我造成什么样子。应该至少像个蜘蛛吧,应该是个好胎没发生什么变异。呵呵...

      

        ”身体造成“如下:

       

代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.IO;
 5 using System.Net;
 6 using System.Collections;
 7 using System.Text.RegularExpressions;
 8 namespace HelloSpider
 9 {
10     class JSpider
11     {
12         private WebClient webClient = new WebClient();
13         private Queue<string> urls = new Queue<string>();
14         private int count = 0;
15         private string current;
16         static void Main(string[] args)
17         {
18             Console.WriteLine("欢迎进入HelloSpider,人家还小爬的比较慢嘛!嘿嘿。。。");
19             JSpider myspider = new JSpider();
20             myspider.urls.Enqueue("http://www.cnblogs.com/yueyue_jwfm/");
21             myspider.Spider();
22             Console.WriteLine("我开始爬行了,速度有点慢哦。");
23         }
24         private void Spider()
25         {
26             while (urls.Count > 0)
27             {
28                 current = urls.Peek();
29                 Console.WriteLine("正在爬行"+urls.Peek()+"页面!");
30                 DownLoad(urls.Dequeue());
31                 Console.WriteLine("爬行结束" + urls.Peek() + "页面!");
32             }
33         }
34         private void DownLoad(string url)
35         {
36             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
37             req.Timeout = 30000;
38             HttpWebResponse response = (HttpWebResponse)req.GetResponse();
39             byte[] buffer = ReadInstreamIntoMemory(response.GetResponseStream());
40             string fileName = count.ToString();
41             FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);
42             fs.Write(buffer, 0, buffer.Length);
43             fs.Close();
44             count++;
45             string html = Encoding.UTF8.GetString(buffer);
46             Parser(html);
47             Spider();
48         }
49         public void Parser(string html)
50         {
51             Console.WriteLine("正在解析" + current + "页面!");
52             string strRef = @"(href|HREF|src|SRC)[ ]*=[ ]*[""'][^""'#>]+[""']";
53             MatchCollection matches = new Regex(strRef).Matches(html);
54             foreach (Match match in matches)
55             {
56                 strRef = match.Value.Substring(match.Value.IndexOf('='+ 1).Trim('"''\'''#'' ''>');
57                 Console.WriteLine("得到了" + strRef + "地址,加到我的大脑里面去!");
58                 urls.Enqueue(strRef);
59             }
60             Console.WriteLine("解析结束" + current + "页面!");
61         }
62         private static byte[] ReadInstreamIntoMemory(Stream stream)
63         {
64             int bufferSize = 16384;
65             byte[] buffer = new byte[bufferSize];
66             MemoryStream ms = new MemoryStream();
67             while (true)
68             {
69                 int numBytesRead = stream.Read(buffer, 0, bufferSize);
70                 if (numBytesRead <= 0break;
71                 ms.Write(buffer, 0, numBytesRead);
72             }
73             return ms.ToArray();
74         }
75     }
76 }
77 

 

        呜呜。。。真的好简单哦,大家别笑我哈。我回头让“妈妈“多吃点东西,长快点。至少像我哥哥那样。这是我们哥俩的照片,”妈妈“拍的。

       

 

应该知道我是那只了...眼睛大吧?嘿嘿...
 
posted @ 2010-04-13 11:26  小军人  阅读(5338)  评论(22编辑  收藏  举报