C# HtmlAgilityPack 爬虫框架

这两天公司不是很忙,在某个网站看见别人爬虫出来的数据感觉很有兴趣就玩了一把,网上找了一个 HtmlAgilityPack 爬虫框架,用了一下感觉很不错

首先从Nuget上面更新Package:HtmlAgilityPack

C# HtmlAgilityPack 爬虫框架

然后就可以开始码代码了,这个框架的具体使用方法详见百度,2333

比如之家的:

private void Ithome()
{
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("https://www.ithome.com/");
HtmlNode node = doc.GetElementbyId("con");
var allnode = node.SelectSingleNode("//*[@class=\"con-block\"]");
var contentnode = allnode.SelectSingleNode("//*[@class=\"rt\"]");
var alltab = contentnode.SelectSingleNode("//*[@class=\"nlst\"]");
var newlist = alltab.SelectSingleNode("//*[@class=\"lst lst-1 new-list\"]");
var div = newlist.ChildNodes[];
foreach (var item in div.ChildNodes)
{
foreach (var li in item.ChildNodes)
{
HtmlNode newnode = HtmlNode.CreateNode(li.OuterHtml);
Console.Write($"{newnode.SelectSingleNode("//*[@class=\"date\"]").InnerText.Trim()}:");
Console.WriteLine($"{newnode.SelectSingleNode("//*[@class=\"title\"]").InnerText.Trim()}");
Console.WriteLine($"地址:{newnode.SelectSingleNode("//*[@class=\"title\"]").ChildNodes[0].Attributes["href"].Value}");
}
Console.WriteLine("========================================");
}
}

C# HtmlAgilityPack 爬虫框架

demo地址:爬虫

上一篇:Ubuntu gcc编译报错:format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]


下一篇:【转】Android 属性动画(Property Animation) 完全解析 (上)