我有:
<div id="foo">
<a href="/xxx.php"> xx </a>
<a href="/xy.php"> xy </a>
<a href="/uid.php?id=123"> 123 </a>
<a href="/uid.php?id=344"> 344 </a>
</div>
我如何使用HtmlAgilityPack僅在href中選擇包含'id'的項目?
輸出:
<a href="/uid.php?id=123"> 123 </a>
<a href="/uid.php?id=344"> 344 </a>
謝謝,高級。
下面的XPath表達式應該選擇所有a
有一個元素href
包含文本“身份證”標籤。
var xpathExpression = "//a[contains(@href, 'id')]";
我可以使用以下代碼在href屬性中選擇帶有id的標籤:
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(
@"<div id=""foo"">
<a href=""/xxx.php""> xx </a>
<a href=""/xy.php""> xy </a>
<a href=""/uid.php?id=123""> 123 </a>
<a href=""/uid.php?id=344""> 344 </a>
</div>");
var aTags = htmlDoc.DocumentNode.SelectNodes("//a[contains(@href, 'id')]");
foreach(var aTag in aTags)
Console.WriteLine(aTag.OuterHtml);