花了幾個小時寫了一個小程式,沒什麼技術含量,第一次寫博客。本人是個菜鳥,想記錄一下自己的學習。 運行效果如圖: 代碼如下: string url = "https://www.cnblogs.com/"; int pagNum = 10; string html = ""; //string pat ...
花了幾個小時寫了一個小程式,沒什麼技術含量,第一次寫博客。本人是個菜鳥,想記錄一下自己的學習。
運行效果如圖:
代碼如下:string url = "https://www.cnblogs.com/"; int pagNum = 10; string html = ""; //string pater = "(<a class=\"titlelnk\" href=\"[\\s\\S]*\" target =\"_blank\">)([\\s\\S]*)(</a>)"; string pater = "<a class=\"titlelnk\" href=\"(.*?)\" target=\"_blank\">(.*?)</a>"; Regex regex = new Regex(pater); int num = 0; for (int i = 1; i < pagNum; i++) { string url2 = url + "#p" + i; try { WebRequest request = WebRequest.Create(url2); Stream stream = request.GetResponse().GetResponseStream(); using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { html = reader.ReadToEnd(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadKey(); } if (!string.IsNullOrEmpty(html)) { // Match match = Regex.Match(html, pater); foreach (Match ma in regex.Matches(html)) { Match math = Regex.Match(ma.Value,pater); string title = math.Groups[2].Value; num++; Console.WriteLine("第" + num + "個標題:\b" +title); File.AppendAllText(@"d:\1.txt",title+ "\r\n"); } } } Console.ReadKey();