用AngleSharp & LINQPad抓取分析博客园排行榜
AngleSharp简单介绍
AngleSharp 是一个
.NET库使您能够解析基于尖括号的超文本,如
HTML、SVG、MathML、XMLAngleSharp的一个重要方面是
CSS也可以解析。同时还是开源,免费的
Github: https://github.com/AngleSharp/AngleSharp使用文档: https://anglesharp.github.io/
开发工具的推荐 LINQPad
介绍:一个小巧,打开秒速,随时能写C#,不至于灵感快速流失的小工具 下载地址:https://www.linqpad.net/有免费版,基本功能已经够用。我们公司买了它的Premium版。
AngleSharp 代码实操
实操前一些分享一些C#的知识点
如何快速发送网络请求获取到数据呢?
可以用如下: 1、HttpWebRequest 2、WebClient 3、HttpClient 4、RestSharp 5、Flurl
本期重点用 HttpClient来实现
起手式
引用NuGet包: Install-PackageAngleSharp
使用场景案例
获取博客园排行榜的Html并且解析
IConfiguration config = Configuration.Default.WithDefaultLoader();string address = "https://www.cnblogs.com/aggsite/SideRight";IBrowsingContext context = BrowsingContext.New(config);IDocument document = await context.OpenAsync(address);IHtmlCollection<IElement> side_right = document.QuerySelectorAll("div");side_right.Select(m => new {title = m.QuerySelector(".card-title a")?.TextContent,url = m.QuerySelectorAll("ul li").Select(x => x.TextContent)}).Where(x => x.title != null).Dump();
通过上面代码快速就能分析且快速抓取博客园的排行榜,简单,快速,高效 代码少,有没有觉得 Linq语法糖配合请求一些框架的强大呢,朋友们
效果图

既然都能抓取数据了,接下来就是爬虫最重要的分析啦
2.分析博客园每天什么时候发博客看的人数最多,点赞的人数最多,星期几发文章多,哪个大佬发文章多
获取数据的方法
通过HttpClient加上Linq加上AngleSharp实现请求获取Hmtl => 保存Json => 分析Json 生成有价值的图表
public void GetData(){var http = new HttpClient();var parser = new HtmlParser();File.WriteAllText(@"C:\Users\QYM\Desktop\OfficFile\BlogData.json", JsonConvert.SerializeObject(Enumerable.Range(1, 200).AsParallel().AsOrdered().SelectMany(page =>{var content = new StringContent(JsonConvert.SerializeObject(new{CategoryId = "808",CategoryType = "SiteHome",ItemListActionName = "AggSitePostList",PageIndex = $"{page}",ParentCategoryId = "0",TotalPostCount = "4000"}), Encoding.UTF8, "application/json");var resp = http.PostAsync("https://www.cnblogs.com/AggSite/AggSitePostList", content).Result;var document = parser.ParseDocument(resp.Content.ReadAsStringAsync().GetAwaiter().GetResult());return document?.QuerySelectorAll("article").Select(pageContext =>{return new{Url = pageContext.QuerySelector(".post-item-text a").GetAttribute("href").Trim(),Title = pageContext.QuerySelector(".post-item-text a").TextContent.Trim(),Context = pageContext.QuerySelector(".post-item-text p").TextContent.Trim(),Name = pageContext.QuerySelector("footer a").TextContent.Trim(),DateTime = DateTime.Parse(pageContext.QuerySelector("footer .post-meta-item").TextContent),LookOK = pageContext.QuerySelector("footer .post-meta-item+a span").TextContent.Trim(),LookPerson = pageContext.QuerySelector("footer .post-meta-item+a+a+a span").TextContent.Trim()};});}), Newtonsoft.Json.Formatting.Indented));}
效果图

获取博客园200页数据
读取数据并且调用LinqPad 自带的Chart图表方法进行分析
public void ReadData(){var data = JsonConvert.DeserializeObject<List<BlogJsonData>>(File.ReadAllText(@"C:\Users\QYM\Desktop\OfficFile\BlogData.json"));Util.Chart(data.GroupBy(x => x.DateTime.Hour).Select(x => new { Hour = x.Key, ViewCount = 1.0 * x.Sum(v => v.LookPerson) }).OrderByDescending(x => x.Hour),x => x.Hour,y => y.ViewCount, Util.SeriesType.Bar).Dump("时间段观看人数最多");Util.Chart(data.GroupBy(x => x.DateTime.Hour).Select(x => new { Hour = x.Key, ViewCount = 1.0 * x.Sum(v => v.LookOk) }).OrderByDescending(x => x.Hour),x => x.Hour,y => y.ViewCount, Util.SeriesType.Bar).Dump("时间段点赞人数最多");Util.Chart(data.GroupBy(x => x.DateTime.DayOfWeek).Select(x => new { WeekDay = x.Key, ArticleCount = x.Count() }).OrderBy(x => x.WeekDay),x => x.WeekDay.ToString(),y => y.ArticleCount, Util.SeriesType.Bar).Dump("星期几发文章最多");Util.Chart(data.GroupBy(x => x.Name).Select(x => new { UserName = x.Key, ArticleCount = x.Count() }).OrderByDescending(x => x.ArticleCount).Take(9),x => x.UserName,y => y.ArticleCount, Util.SeriesType.Bar).Dump("哪个大佬发文章比较多");}
效果图
源文件Json

分析数据的实体
public class BlogJsonData{public string Url { get; set; }public string Title { get; set; }public string Context { get; set; }public string Name { get; set; }public DateTime DateTime { get; set; }public int LookOk { get; set; }public int LookPerson {get;set;}}
接下来就是见证奇迹的时候,通过分析抓取到html,保存成Json分析出一些意想不到的图表
效果图

时间段观看人数最多?

看来博客园一般查看人数最多的是9点->10点,说明哈哈哈,果然大家早上都是喜欢关注编程的大事呀
时间段点赞人数最多?
果然早起的鸟儿有虫吃,如果想要博客点赞高,那就必须早上九点 -> 10点 抓住阅读高峰期,菜鸟收获高赞,得到很多人的认可
星期几发文章最多?

看来星期一到星期五中发博客最多的星期一和星期二,最不想上班的两天最适合用来灵感创作写文字
哪个大佬发文章比较多?

目测近期京东云开发者对博客园贡献很大,看了一下,质量都是很高的文章,极力推荐

