如何使用 XPATH 从 HTML 中查找属性
How to find attribute from HTML using XPATH
我正在尝试从 html 检索文本,所以首先加载 html
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = web.Load(url);
此处要查找的文本是 0.802 并且直接在元素中并且有效
为此 html
<tr>
<th>Reported-Normalized Volume Ratio</th>
<td>0.802</td>
</tr>
搜索字符串
var t5 = doc.DocumentNode.SelectSingleNode("//th[.='Reported-Normalized Volume Ratio']/following-sibling::td").InnerText;
t5 的值为预期的 0.802
现在我需要找到 data-price-btc="3535.5500891201248734"
<tr>
<th style="width: 300px;">Reported Trading Volume</th>
<td><div data-target="price.price" data-price-btc="3535.5500891201248734">
</div></td>
</tr>
搜索字符串
var t1 = doc.DocumentNode.SelectSingleNode("//th[.='Reported Trading Volume']/following-sibling::td/div/@data-price-btc");
t1 是空的,我不明白为什么
问题是 HTML Agility Pack
不支持直接选择属性
我正在尝试从 html 检索文本,所以首先加载 html
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = web.Load(url);
此处要查找的文本是 0.802 并且直接在元素中并且有效 为此 html
<tr>
<th>Reported-Normalized Volume Ratio</th>
<td>0.802</td>
</tr>
搜索字符串
var t5 = doc.DocumentNode.SelectSingleNode("//th[.='Reported-Normalized Volume Ratio']/following-sibling::td").InnerText;
t5 的值为预期的 0.802
现在我需要找到 data-price-btc="3535.5500891201248734"
<tr>
<th style="width: 300px;">Reported Trading Volume</th>
<td><div data-target="price.price" data-price-btc="3535.5500891201248734">
</div></td>
</tr>
搜索字符串
var t1 = doc.DocumentNode.SelectSingleNode("//th[.='Reported Trading Volume']/following-sibling::td/div/@data-price-btc");
t1 是空的,我不明白为什么
问题是 HTML Agility Pack 不支持直接选择属性