GetElementById return 不正确的节点

GetElementById return incorrect nodes

我在解析 this 页面时遇到了一个小问题(我不知道是我做错了什么还是 bug)。

我正在尝试获取此 table 中可用的所有 a 标签:

所以为了实现这个,我写了这段代码:

`var d = doc.GetElementbyId("odds-data-table");
HtmlNodeCollection listItems = d.SelectNodes("//a");`

特别是 d 包含我想要的 table 结构:

但是 listItems 变量不包含 table 的 link,而是整个 html 页面的 link,这很奇怪。我尝试了不同的大小写:

d.SelectNodes("a") : return null
d.SelectNodes("//a") : return all the link of the page 
d.SelectNodes("/a") : return null

怎么了? 另外想问下HtmlAgilityPack文档用的是什么插件或者系统,真的很厉害,谢谢

你必须阅读 Attributes 属性 你的每个 HtmlNode 来自你的 HtmlNodeCollection

HtmlDocument doc = new HtmlDocument();
var d = doc.GetElementbyId("odds-data-table");
HtmlNodeCollection listItems = d.SelectNodes(".//a");

//This list contains all your href values
List<string> hrefs = new List<string>();
foreach (var item in listItems)
{
      var href = item.Attributes["href"].Value;
      hrefs.Add(href);
}