Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTML敏捷性-在下面的代码中只找到一条记录_C#_Html Agility Pack - Fatal编程技术网

C# HTML敏捷性-在下面的代码中只找到一条记录

C# HTML敏捷性-在下面的代码中只找到一条记录,c#,html-agility-pack,C#,Html Agility Pack,我正在尝试获取某个类的所有div标记 下面的代码运行正常,但只返回一条记录 我做错了什么 using (WebClient client = new WebClient()) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); string html = client.DownloadStri

我正在尝试获取某个类的所有div标记

下面的代码运行正常,但只返回一条记录

我做错了什么

            using (WebClient client = new WebClient())
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                string html = client.DownloadString("https://myurl.com");

                doc.LoadHtml(html);

                var findDivs = doc.DocumentNode.Descendants().Where(d =>
                            d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("list-mode-table-wrapper")
                        ).Select(x => x).ToList();
            }

我建议使用SelectNodes获取具有特定类名的所有div标记

    var findDivs = doc.DocumentNode.SelectNodes("//div[@class='list-mode-table-wrapper']")?.ToList();
SelectNodes使用XPath在使用//时搜索整个文档。这将在整个页面中搜索具有该类名的div。对于该类的div下的任何内容,都可以使用/指定它下需要的元素(
(“//div[@class='xyz']/table/tbody/etc”)


由于如果未找到任何内容,SelectNodes将返回null,因此在继续使用它时,可以使用错误检查来确保findDivs不为null。

我尝试过,但仍然有一条记录。网站能否阻止html敏捷性工作?这个表行在每个div标记下面:primary row tr您是否实际检查了
doc.DocumentNode
的内部HTML以查看是否有两个具有相同类名的div?我尝试了这个'var findDivs=doc.DocumentNode.SelectNodes(“//div[@class='list-mode-table-wrapper']/table/tbody/tr[@class='primary-row-tr'])。ToList()`如果不看这份文件,我就没有什么可以建议的了。使用
debugger
Immediate window
查看每个查询得到的节点数(//div[],然后重试,//div[class]/table)。检查innerHtml以查看实际文档并继续向下。如果中间有额外的标记,请使用
/
而不是单个
/
您是对的!只有一个。但在F12的检查中有31个。我错过了什么?