【发布时间】:2018-04-13 14:06:58
【问题描述】:
我发现 HtmlAgilityPack SelectSingleNode 总是从原始 DOM 的第一个节点开始。是否有等效的方法来设置其起始节点?
示例 html
<html>
<body>
<a href="https://home.com">Home</a>
<div id="contentDiv">
<tr class="blueRow">
<td scope="row"><a href="https://iwantthis.com">target</a></td>
</tr>
</div>
</body>
</html>
代码无效
//Expected:iwantthis.com Actual:home.com,
string url = contentDiv.SelectSingleNode("//tr[@class='blueRow']")
.SelectSingleNode("//a") //What should this be ?
.GetAttributeValue("href", "");
我必须用这个替换上面的代码:
var tds = contentDiv.SelectSingleNode("//tr[@class='blueRow']").Descendants("td");
string url = "";
foreach (HtmlNode td in tds)
{
if (td.Descendants("a").Any())
{
url= td.ChildNodes.First().GetAttributeValue("href", "");
}
}
我在 .Net Framework 4.6.2 上使用 HtmlAgilityPack 1.7.4
【问题讨论】: