Asp.net 使用HtmlAgilityPack Web Scraping c提取数据#

Asp.net 使用HtmlAgilityPack Web Scraping c提取数据#,asp.net,.net,web-scraping,html-agility-pack,Asp.net,.net,Web Scraping,Html Agility Pack,它在selectnodes中返回null的原因可能是什么 HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load("https://www.wired.com/most-popular/"); var headerNames = doc.DocumentNode.SelectNodes("//a[@class='archive-item-component__title']").ToList();

它在selectnodes中返回null的原因可能是什么

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("https://www.wired.com/most-popular/");
var headerNames = doc.DocumentNode.SelectNodes("//a[@class='archive-item-component__title']").ToList();
string listData = "";
foreach(var item in headerNames)
{
   listData += Environment.NewLine + item.InnerText;   
}
Console.WriteLine(listData);

原因是网站中的“archive-item-component\uuuu title”类是为h2标记指定的,而不是为a标记指定的

像这样选择您的总部:

doc.DocumentNode.SelectNodes("//h2[@class='archive-item-component__title']");

PS:您不需要对SelectNodes结果调用.ToList(),您可以按原样循环HtmlNodeCollection。

1。不要使用HtmlAgilityPack,请使用AngleSharp。2最有可能的情况是,该页面是使用React之类的客户端呈现构建的,因此您无法对其进行刮取。