我想制作一个控制台应用程序,当我输入他们的 channel 时返回他们有多少订阅者。我目前有它从 Console.ReadLine() 下载搜索页面作为 html。这会返回一个成功的 html 页面,因为我添加了一个 .Save 以查看下载是否有任何输出。但是,当使用 xcode 抓取 html 时,它返回 null。我曾多次尝试使用 Google Chrome 副本 xcode,但没有成功。
这是我目前的代码:
public static void GetYoutubeSubscriptions2()
{
string url = "https://www.youtube.com/results?search_query=" + Regex.Replace(Console.ReadLine(), @"\s+", "+");
Console.WriteLine(url);
HtmlWeb web = new HtmlWeb();
HtmlDocument document = web.Load(url);
//document.Save(path1);
var htmlNodes = document.DocumentNode.SelectNodes(@"//*[@id=""section - list - 231763""]/li[3]/div/div[2]/div[3]/span/span[1]");
foreach (var node in htmlNodes)
{
Console.WriteLine(node.InnerText);
}
Console.ReadLine();
}
如有任何帮助建议,我们将不胜感激! :)
最佳答案
这是你需要的:
var htmlNodes = document.DocumentNode.Descendants("span").FirstOrDefault(_ => _.HasClass("yt-subscriber-count"));
这是 HasClass
扩展
public static bool HasClass(this HtmlNode node, params string[] classValueArray)
{
var classValue = node.GetAttributeValue("class", "");
var classValues = classValue.Split(' ');
return classValueArray.All(c => classValues.Contains(c));
}
关于C# 和 HTML 敏捷包 - 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44553604/