C#簡單的爬蟲,爬博客園首頁文章標題


運行效果如圖:

 

 

 代碼如下:
 1 using System;
 2 using System.IO;
 3 using System.Net;
 4 using System.Text;
 5 using System.Text.RegularExpressions;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string host = "https://www.cnblogs.com/?";  //url后面必須加一個?不然程序請求都是404,不知道是什么原因
14             int num = 0; //統計當前為第幾個文章的標題
15             int pagSize = 100; //爬取的最大頁數 10表示爬首頁前10頁的標題
16 
17             //標題標簽樣例:<a class="titlelnk" href="https://www.xxxxx.html" target="_blank">【設計模式】簡單工廠模式 Simple Factory Pattern</a>
18             string pater = "<a class=\"titlelnk\" href=\"(.*?)\" target=\"_blank\">(.*?)</a>";  //()為C#要捕捉的內容,括號里面的".*?"表示匹配任意內容(因為url的地址是不確定的)
19             Regex regex = new Regex(pater);
20 
21             for (int i = 1; i < pagSize; i++)
22             {
23                 //首頁完整鏈接為https://www.cnblogs.com/#p2  #p后面的數字代表當前頁
24                 string url = host + "#p" + i;
25                 var html = GetHtmlString(url);
26                 if (!string.IsNullOrEmpty(html))
27                 {
28                     //標題標簽<a class="titlelnk" href="https://www.xxxxx.html" target="_blank">【設計模式】簡單工廠模式 Simple Factory Pattern</a>
29                     //正則匹配標題的標簽,再提取其中的名稱和url
30                     foreach (Match ma in regex.Matches(html))
31                     {
32                         Match match = Regex.Match(ma.Value, pater);
33                         string title = match.Groups[2].Value;
34                         string titlelnk = match.Groups[1].Value;
35                         Console.WriteLine($"-------------------------------第{ ++num }個標題------------------------");
36                         Console.WriteLine(title + "Url:" + titlelnk);
37                         Console.WriteLine("--------------------------------------------------------------------------");
38                         File.AppendAllText(@"d:\cnblog.txt", title + "     " + titlelnk + "\r\n");
39                     }
40                 }
41             }
42             Console.WriteLine("結束一共爬了" + num + "個標題");
43             Console.ReadKey();
44         }
45 
46         /// <summary>
47         /// 請求url
48         /// </summary>
49         /// <param name="url"></param>
50         /// <returns></returns>
51         public static string GetHtmlString(string url)
52         {
53             try
54             {
55                 WebRequest request = WebRequest.Create(url);
56                 Stream stream = request.GetResponse().GetResponseStream();
57                 request.Timeout = 3000;
58                 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
59                 {
60                     return reader.ReadToEnd();
61                 }
62             }
63             catch (Exception ex)
64             {
65                 Console.WriteLine(ex.ToString());
66                 return null;
67             }
68         }
69 
70     }
71 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM