派生用于访问 table 元素的 XPath

Deriving XPath for accessing table elements

我正在尝试使用 HtmlAgilityPack 和 C# 控制台应用从这个 website 中抓取 table。

我能够抓取第 2 列中的股票名称(例如:EDAP TMS ADR (EDAP),但我无法从 Price、Chg、Chg 中获取任何值的正确 XPath % 列。

例如: 我的 names 列的 XPath 完美地工作为:

"//*[@id=\"column0\"]//div//table//tr//td//a"

Price、Chg、Chg% 列的 XPath 是什么? 你能帮我理解你是如何推导出来的吗?

这是根据列名获取所需列输出的 xpath。

对于价格:从第 4 行获取价格。

//div[@class='mdcNarrowM']//table//tr[4]/td[count(ancestor::table[1]//tr[1]/td[.='Price']/preceding-sibling::td)+1]

本例中的一般表示法:(根据需要更新行号和列名)测试了 table 中的所有列。

//div[@class='mdcNarrowM']//table//tr[row_number_goes_here]/td[count(ancestor::table[1]//tr[1]/td[.='column name goes here']/preceding-sibling::td)+1]

要获取所有行(header 行除外),请在 xpath 下使用它。

//div[@class='mdcNarrowM']//table//tr[not(td[@class='colhead'])]/td[count(ancestor::table[1]//tr[1]/td[.='Price']/preceding-sibling::td)+1]