使用 HtmlAgilityPack Web Scraping c# 提取数据
pull data with HtmlAgilityPack Web Scraping c#
在 selectnodes 中 returns 为空的原因可能是什么?
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__title'class指定的是h2标签,不是a标签。
像这样选择 headerNames 有效:
doc.DocumentNode.SelectNodes("//h2[@class='archive-item-component__title']");
PS:您不需要在 SelectNodes 结果上调用 .ToList(),您可以按原样循环遍历 HtmlNodeCollection。
在 selectnodes 中 returns 为空的原因可能是什么?
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__title'class指定的是h2标签,不是a标签。
像这样选择 headerNames 有效:
doc.DocumentNode.SelectNodes("//h2[@class='archive-item-component__title']");
PS:您不需要在 SelectNodes 结果上调用 .ToList(),您可以按原样循环遍历 HtmlNodeCollection。