花了好多个钟头写了一个微信小程序,没有什么科技含量,第一次写文章赚钱。自己是个小白,想纪录一下自身的学习培训。

运作实际效果如图所示:

简单的爬虫,爬取博客园文章标题 数据库 第1张
 
编码以下:
            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();