HTMLAgilityPack 如何解析 HTML 目录中的特定属性
HTMLAgilityPack How to parse a specific attribute in HTML Contents
任何人都可以帮助我如何解析特定属性(如果我理解正确的话)。以下是示例。
<div class="buy-container">
<form action="/content/action" method="post">
<button class="product-buy">buy</button>
<input type="hidden" name="ActionAddToBasket">
<input type="hidden" value="198300" name="ContentNodeID">
<input type="hidden" value="778768" name="ContentObjectID">
<input type="hidden" value="full" name="ViewMode">
</form>
</div>
我想从上面的 html 数据中解析 where value = "778768" 。
请指教。非常感谢。
你可以这样做。
var val = doc
.DocumentNode
.SelectSingleNode("//input[@type='hidden' and @name='ContentObjectID']")
.Attributes["value"].Value;
勾选这个fiddle demo
试试这个:
var html = @"
<div class=""buy-container"">
<form action=""/content/action"" method=""post"">
<button class=""product-buy"">buy</button>
<input type=""hidden"" name=""ActionAddToBasket"">
<input type=""hidden"" value=""198300"" name=""ContentNodeID"">
<input type=""hidden"" value=""778768"" name=""ContentObjectID"">
<input type=""hidden"" value=""full"" name=""ViewMode"">
</form>
</div>
";
var doc = new HtmlDocument();
doc.LoadHtml(html);
var input = doc.DocumentNode.SelectSingleNode("//input[@name = 'ContentObjectID']");
// your desired value
var val = input.GetAttributeValue("value", "");
任何人都可以帮助我如何解析特定属性(如果我理解正确的话)。以下是示例。
<div class="buy-container">
<form action="/content/action" method="post">
<button class="product-buy">buy</button>
<input type="hidden" name="ActionAddToBasket">
<input type="hidden" value="198300" name="ContentNodeID">
<input type="hidden" value="778768" name="ContentObjectID">
<input type="hidden" value="full" name="ViewMode">
</form>
</div>
你可以这样做。
var val = doc
.DocumentNode
.SelectSingleNode("//input[@type='hidden' and @name='ContentObjectID']")
.Attributes["value"].Value;
勾选这个fiddle demo
试试这个:
var html = @"
<div class=""buy-container"">
<form action=""/content/action"" method=""post"">
<button class=""product-buy"">buy</button>
<input type=""hidden"" name=""ActionAddToBasket"">
<input type=""hidden"" value=""198300"" name=""ContentNodeID"">
<input type=""hidden"" value=""778768"" name=""ContentObjectID"">
<input type=""hidden"" value=""full"" name=""ViewMode"">
</form>
</div>
";
var doc = new HtmlDocument();
doc.LoadHtml(html);
var input = doc.DocumentNode.SelectSingleNode("//input[@name = 'ContentObjectID']");
// your desired value
var val = input.GetAttributeValue("value", "");