用AngleSharp & LINQPad抓取分析博客園排行榜
AngleSharp簡(jiǎn)單介紹
AngleSharp?是一個(gè)?
.NET庫(kù)
使您能夠解析基于尖括號(hào)的超文本,如?
HTML、SVG、MathML、XML
AngleSharp的一個(gè)重要方面是?
CSS也可以解析
。同時(shí)還是開(kāi)源,免費(fèi)的
Github: https://github.com/AngleSharp/AngleSharp使用文檔: https://anglesharp.github.io/
開(kāi)發(fā)工具的推薦?LINQPad
介紹:一個(gè)小巧,打開(kāi)秒速,隨時(shí)能寫(xiě)C#,不至于靈感快速流失的小工具 下載地址:https://www.linqpad.net/有免費(fèi)版,基本功能已經(jīng)夠用。我們公司買(mǎi)了它的Premium版。
AngleSharp 代碼實(shí)操
實(shí)操前一些分享一些C#的知識(shí)點(diǎn)
如何快速發(fā)送網(wǎng)絡(luò)請(qǐng)求獲取到數(shù)據(jù)呢?
可以用如下: 1、HttpWebRequest 2、WebClient 3、HttpClient 4、RestSharp 5、Flurl
本期重點(diǎn)用 HttpClient
來(lái)實(shí)現(xiàn)
起手式
引用NuGet包: Install-PackageAngleSharp
使用場(chǎng)景案例
獲取博客園排行榜的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();
通過(guò)上面代碼快速就能分析且快速抓取博客園的排行榜,簡(jiǎn)單,快速,高效 代碼少,有沒(méi)有覺(jué)得 Linq語(yǔ)法糖配合請(qǐng)求一些框架的強(qiáng)大呢,朋友們
效果圖

既然都能抓取數(shù)據(jù)了,接下來(lái)就是爬蟲(chóng)最重要的分析啦
2.分析博客園每天什么時(shí)候發(fā)博客看的人數(shù)最多,點(diǎn)贊的人數(shù)最多,星期幾發(fā)文章多,哪個(gè)大佬發(fā)文章多
獲取數(shù)據(jù)的方法
通過(guò)HttpClient加上Linq加上AngleSharp實(shí)現(xiàn)請(qǐng)求獲取Hmtl => 保存Json => 分析Json 生成有價(jià)值的圖表
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頁(yè)數(shù)據(jù)
讀取數(shù)據(jù)并且調(diào)用LinqPad 自帶的Chart圖表方法進(jìn)行分析
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("時(shí)間段觀看人數(shù)最多");
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("時(shí)間段點(diǎn)贊人數(shù)最多");
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("星期幾發(fā)文章最多");
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("哪個(gè)大佬發(fā)文章比較多");
}
效果圖
源文件Json

分析數(shù)據(jù)的實(shí)體
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;}
}
接下來(lái)就是見(jiàn)證奇跡的時(shí)候,通過(guò)分析抓取到html,保存成Json分析出一些意想不到的圖表
效果圖

時(shí)間段觀看人數(shù)最多??

看來(lái)博客園一般查看人數(shù)最多的是9點(diǎn)->10點(diǎn),說(shuō)明哈哈哈,果然大家早上都是喜歡關(guān)注編程的大事呀
時(shí)間段點(diǎn)贊人數(shù)最多??
果然早起的鳥(niǎo)兒有蟲(chóng)吃,如果想要博客點(diǎn)贊高,那就必須早上九點(diǎn) -> 10點(diǎn) 抓住閱讀高峰期,菜鳥(niǎo)收獲高贊,得到很多人的認(rèn)可
星期幾發(fā)文章最多??

看來(lái)星期一到星期五中發(fā)博客最多的星期一和星期二,最不想上班的兩天最適合用來(lái)靈感創(chuàng)作寫(xiě)文字
哪個(gè)大佬發(fā)文章比較多??

目測(cè)近期京東云開(kāi)發(fā)者對(duì)博客園貢獻(xiàn)很大,看了一下,質(zhì)量都是很高的文章,極力推薦