gpt4 book ai didi

C# 和 HTML 敏捷包 - 返回空值

转载 作者:行者123 更新时间:2023-11-28 02:40:57 25 4
gpt4 key购买 nike

我想制作一个控制台应用程序,当我输入他们的 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/

25 4 0
文章推荐: jquery - 在 Html 中查找关键字并使用 jQuery 隐藏特定内容
文章推荐: c# - 将 HTML 文本区域/文本框中的文本读取到 C# 代码隐藏中。 (ASP.NET)
文章推荐: c++ - 将引用传递给函数是否可以进行内存管理?
文章推荐: html - css 文本装饰 :none; in chrome with inside of
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com