博客园爬虫模拟

  

  /*
             原理分析: 
             1.通过抓包工具 分析请求地址:http://www.cnblogs.com/liuxiaoji/p/4689119.html
             2.可以看出这个请求是GET请求
             3.通过http请求把数据抓取回来
             4.HttpHelper帮助类请联系作者购买
            */
            HttpHelper http = new HttpHelper();
            string htmlText = http.HttpGet("http://www.cnblogs.com/liuxiaoji/p/4689119.html",string.Empty, Encoding.UTF8, false, false, 5000);


            // 正则css路径分析 
            Regex linkCss = new Regex(@"<link[^<>]*?href[s	
]*=[s	
]*[""']?[s	
]*(?<url>[^s	
""'<>]*)[^<>]*?/?[s	
]*>", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串 
            MatchCollection matches = linkCss.Matches(htmlText);

            // 取得匹配项列表 
            foreach (Match match in matches)
            {
                var item = match.Groups["url"].Value;
                if (!item.Contains("http://www.cnblogs.com"))
                {
                    htmlText = htmlText.Replace(item, item.Contains("/skins") ? $"http://www.cnblogs.com{item}" : $"http://www.cnblogs.com/skins{item}");
                }
            }

            // 最终结果
            var result = htmlText;
            // 文件保存
            using (FileStream fs = new FileStream("E:\liuxiaoji.html", FileMode.Create))
            {
                var data = Encoding.UTF8.GetBytes(result);
                fs.Write(data, 0, data.Length);
            }
原文地址:https://www.cnblogs.com/liuxiaoji/p/6956015.html