基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(二)
基于 abp vNext 和 .NET Core 開發博客項目 - 定時任務最佳實戰(二)
轉載于:https://github.com/Meowv/Blog
本篇繼續來完成一個全網各大平臺的熱點新聞數據的抓取。
同樣的,可以先預覽一下我個人博客中的成品:https://meowv.com/hot 😝😝😝,和抓取壁紙的套路一樣,大同小異。
圖片
本次要抓取的源有18個,分別是博客園、V2EX、SegmentFault、掘金、微信熱門、豆瓣精選、IT之家、36氪、百度貼吧、百度熱搜、微博熱搜、知乎熱榜、知乎日報、網易新聞、GitHub、抖音熱點、抖音視頻、抖音正能量。
還是將數據存入數據庫,按部就班先將實體類和自定義倉儲創建好,實體取名HotNews。貼一下代碼:
//HotNews.cs
using System;
using Volo.Abp.Domain.Entities;
namespace Meowv.Blog.Domain.HotNews
{
public class HotNews : Entity
{
///
/// 標題
///
public string Title { get; set; }
}
剩下的大家自己完成,最終數據庫生成一張空的數據表,meowv_hotnews 。
圖片
然后還是將我們各大平臺放到一個枚舉類HotNewsEnum.cs中。
//HotNewsEnum.cs
using System.ComponentModel;
namespace Meowv.Blog.Domain.Shared.Enum
{
public enum HotNewsEnum
{
[Description(“博客園”)]
cnblogs = 1,
}
和上一篇抓取壁紙一樣,做一些準備工作。
在.Application.Contracts層添加HotNewsJobItem,在.BackgroundJobs層添加HotNewsJob用來處理爬蟲邏輯,用構造函數方式注入倉儲IHotNewsRepository。
//HotNewsJobItem.cs
using Meowv.Blog.Domain.Shared.Enum;
namespace Meowv.Blog.Application.Contracts.HotNews
{
public class HotNewsJobItem
{
///
///
///
public T Result { get; set; }
}
//HotNewsJob.CS
using Meowv.Blog.Domain.HotNews.Repositories;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace Meowv.Blog.BackgroundJobs.Jobs.HotNews
{
public class HotNewsJob : IBackgroundJob
{
private readonly IHttpClientFactory _httpClient;
private readonly IHotNewsRepository _hotNewsRepository;
}
接下來明確數據源地址,因為以上數據源有的返回是HTML,有的直接返回JSON數據。為了方便調用,我這里還注入了IHttpClientFactory。
整理好的待抓取數據源列表是這樣的。
…
var hotnewsUrls = new List<HotNewsJobItem>
{
new HotNewsJobItem { Result = “https://www.cnblogs.com”, Source = HotNewsEnum.cnblogs },
new HotNewsJobItem { Result = “https://www.v2ex.com/?tab=hot”, Source = HotNewsEnum.v2ex },
new HotNewsJobItem { Result = “https://segmentfault.com/hottest”, Source = HotNewsEnum.segmentfault },
new HotNewsJobItem { Result = “https://web-api.juejin.im/query”, Source = HotNewsEnum.juejin },
new HotNewsJobItem { Result = “https://weixin.sogou.com”, Source = HotNewsEnum.weixin },
new HotNewsJobItem { Result = “https://www.douban.com/group/explore”, Source = HotNewsEnum.douban },
new HotNewsJobItem { Result = “https://www.ithome.com”, Source = HotNewsEnum.ithome },
new HotNewsJobItem { Result = “https://36kr.com/newsflashes”, Source = HotNewsEnum.kr36 },
new HotNewsJobItem { Result = “http://tieba.baidu.com/hottopic/browse/topicList”, Source = HotNewsEnum.tieba },
new HotNewsJobItem { Result = “http://top.baidu.com/buzz?b=341”, Source = HotNewsEnum.baidu },
new HotNewsJobItem { Result = “https://s.weibo.com/top/summary/summary”, Source = HotNewsEnum.weibo },
new HotNewsJobItem { Result = “https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true”, Source = HotNewsEnum.zhihu },
new HotNewsJobItem { Result = “https://daily.zhihu.com”, Source = HotNewsEnum.zhihudaily },
new HotNewsJobItem { Result = “http://news.163.com/special/0001386F/rank_whole.html”, Source = HotNewsEnum.news163 },
new HotNewsJobItem { Result = “https://github.com/trending”, Source = HotNewsEnum.github },
new HotNewsJobItem { Result = “https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/word”, Source = HotNewsEnum.douyin_hot },
new HotNewsJobItem { Result = “https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/aweme”, Source = HotNewsEnum.douyin_video },
new HotNewsJobItem { Result = “https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/aweme/?type=positive”, Source = HotNewsEnum.douyin_positive },
};
…
其中有幾個比較特殊的,掘金、百度熱搜、網易新聞。
掘金需要發送Post請求,返回的是JSON數據,并且需要指定特有的請求頭和請求數據,所以使用IHttpClientFactory創建了HttpClient對象。
百度熱搜、網易新聞兩個老大哥玩套路,網頁編碼是GB2312的,所以要專門為其指定編碼方式,不然取到的數據都是亂碼。
…
var web = new HtmlWeb();
var list_task = new List<Task<HotNewsJobItem>>();
hotnewsUrls.ForEach(item =>
{
var task = Task.Run(async () =>
{
var obj = new object();
});
Task.WaitAll(list_task.ToArray());
循環 hotnewsUrls ,可以看到HotNewsJobItem我們返回的是object類型,因為有JSON又有HtmlDocument對象。所以這里為了能夠統一接收,就是用了object。
針對掘金做了單獨處理,使用HttpClient發送Post請求,返回JSON字符串數據。
針對百度熱搜和網易新聞,使用Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);注冊編碼提供程序,然后在web.LoadFromWebAsync(…)加載網頁數據的時候指定網頁編碼,我使用了一個三元表達式來處理。
完成上面這一步,就可以循環 list_task,使用XPath語法,或者解析JSON數據,去拿到數據了。
…
var hotNews = new List();
foreach (var list in list_task)
{
var item = await list;
var sourceId = (int)item.Source;
}
這個爬蟲同樣很簡單,只要拿到標題和鏈接即可,所以主要目標是尋找到頁面上的a標簽列表。這個我覺得也沒必要一個個去分析了,直接上代碼。
// 博客園
if (item.Source == HotNewsEnum.cnblogs)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘post_item_body’]/h3/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// V2EX
if (item.Source == HotNewsEnum.v2ex)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//span[@class=‘item_title’]/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = $“https://www.v2ex.com{x.GetAttributeValue(“href”, “”)}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// SegmentFault
if (item.Source == HotNewsEnum.segmentfault)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘news__item-info clearfix’]/a").Where(x => x.InnerText.IsNotNullOrEmpty()).ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.SelectSingleNode(".//h4").InnerText,
Url = $“https://segmentfault.com{x.GetAttributeValue(“href”, “”)}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 掘金
if (item.Source == HotNewsEnum.juejin)
{
var obj = JObject.Parse((string)item.Result);
var nodes = obj[“data”][“articleFeed”][“items”][“edges”];
foreach (var node in nodes)
{
hotNews.Add(new HotNews
{
Title = node[“node”][“title”].ToString(),
Url = node[“node”][“originalUrl”].ToString(),
SourceId = sourceId,
CreateTime = DateTime.Now
});
}
}
// 微信熱門
if (item.Source == HotNewsEnum.weixin)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//ul[@class=‘news-list’]/li/div[@class=‘txt-box’]/h3/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 豆瓣精選
if (item.Source == HotNewsEnum.douban)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘channel-item’]/div[@class=‘bd’]/h3/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// IT之家
if (item.Source == HotNewsEnum.ithome)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘lst lst-2 hot-list’]/div[1]/ul/li/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 36氪
if (item.Source == HotNewsEnum.kr36)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘hotlist-main’]/div[@class=‘hotlist-item-toptwo’]/a[2]|//div[@class=‘hotlist-main’]/div[@class=‘hotlist-item-other clearfloat’]/div[@class=‘hotlist-item-other-info’]/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = $“https://36kr.com{x.GetAttributeValue(“href”, “”)}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 百度貼吧
if (item.Source == HotNewsEnum.tieba)
{
var obj = JObject.Parse(((HtmlDocument)item.Result).ParsedText);
var nodes = obj[“data”][“bang_topic”][“topic_list”];
foreach (var node in nodes)
{
hotNews.Add(new HotNews
{
Title = node[“topic_name”].ToString(),
Url = node[“topic_url”].ToString().Replace(“amp;”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
}
}
// 百度熱搜
if (item.Source == HotNewsEnum.baidu)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//table[@class=‘list-table’]//tr/td[@class=‘keyword’]/a[@class=‘list-title’]").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 微博熱搜
if (item.Source == HotNewsEnum.weibo)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//table/tbody/tr/td[2]/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = $“https://s.weibo.com{x.GetAttributeValue(“href”, “”).Replace(”#", “%23”)}",
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 知乎熱榜
if (item.Source == HotNewsEnum.zhihu)
{
var obj = JObject.Parse(((HtmlDocument)item.Result).ParsedText);
var nodes = obj[“data”];
foreach (var node in nodes)
{
hotNews.Add(new HotNews
{
Title = node[“target”][“title”].ToString(),
Url = $“https://www.zhihu.com/question/{node[“target”][“id”]}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
}
}
// 知乎日報
if (item.Source == HotNewsEnum.zhihudaily)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘box’]/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = $“https://daily.zhihu.com{x.GetAttributeValue(“href”, “”)}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 網易新聞
if (item.Source == HotNewsEnum.news163)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//div[@class=‘area-half left’]/div[@class=‘tabBox’]/div[@class=‘tabContents active’]/table//tr/td[1]/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText,
Url = x.GetAttributeValue(“href”, “”),
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// GitHub
if (item.Source == HotNewsEnum.github)
{
var nodes = ((HtmlDocument)item.Result).DocumentNode.SelectNodes("//article[@class=‘Box-row’]/h1/a").ToList();
nodes.ForEach(x =>
{
hotNews.Add(new HotNews
{
Title = x.InnerText.Trim().Replace("\n", “”).Replace(" “, “”),
Url = $“https://github.com{x.GetAttributeValue(“href”, “”)}”,
SourceId = sourceId,
CreateTime = DateTime.Now
});
});
}
// 抖音熱點
if (item.Source == HotNewsEnum.douyin_hot)
{
var obj = JObject.Parse(((HtmlDocument)item.Result).ParsedText);
var nodes = obj[“word_list”];
foreach (var node in nodes)
{
hotNews.Add(new HotNews
{
Title = node[“word”].ToString(),
Url = $”#{node[“hot_value”]}",
SourceId = sourceId,
CreateTime = DateTime.Now
});
}
}
// 抖音視頻 & 抖音正能量
if (item.Source == HotNewsEnum.douyin_video || item.Source == HotNewsEnum.douyin_positive)
{
var obj = JObject.Parse(((HtmlDocument)item.Result).ParsedText);
var nodes = obj[“aweme_list”];
foreach (var node in nodes)
{
hotNews.Add(new HotNews
{
Title = node[“aweme_info”][“desc”].ToString(),
Url = node[“aweme_info”][“share_url”].ToString(),
SourceId = sourceId,
CreateTime = DateTime.Now
});
}
}
將item.Result轉換成指定類型,最終拿到數據后,我們先刪除所有數據后再批量插入。
然后新建擴展方法UseHotNewsJob(),在模塊類中調用。
//MeowvBlogBackgroundJobsExtensions.cs
…
///
/// 每日熱點數據抓取
///
///
public static void UseHotNewsJob(this IServiceProvider service)
{
var job = service.GetService();
…
指定定時任務為每2小時運行一次。
…
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
…
var service = context.ServiceProvider;
…
service.UseHotNewsJob();
}
編譯運行,此時周期性作業就會出現我們的定時任務了。
圖片
默認時間沒到是不會執行的,我們手動執行等待一會看看效果。
圖片
圖片
執行完成后,成功將所有熱點數據保存在數據庫中,說明我們的爬蟲已經搞定了,并且Hangfire會按照給定的規則去循環執行,你學會了嗎?😁😁😁
開源地址:https://github.com/Meowv/Blog/tree/blog_tutorial
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于 abp vNext 和 .NET
- 下一篇: 基于 abp vNext 和 .NET