问题
I am trying to parse the first link in the html code below /search?id=3
<div class="brs_col">
<p>
<a href="/search?id=3">
<b>
vastu shastra
</b>
</a>
</p>
<p>
<a href="/search?id=1">
<b>
bygga
</b>
bastu
</a>
</p>
</div>
I've tried to select it with the following XPATH, but cant seem to get any of them to work:
//div[@class='brs_col']//p//a[@href]
//div[@class='brs_col']//p[0]//a[@href]
//div[@class='brs_col']//p//a[0][@href]
Any ideas?
回答1:
Try this:
var doc = new HtmlDocument();
doc.LoadHtml(@"<div class=""brs_col"">
<p><a href=""/search?id=3""><b>vastu shastra</b></a></p>
<p><a href=""/search?id=1""><b>bygga</b>bastu</a></p>
</div>");
var hrefValue = doc.DocumentNode
.SelectSingleNode("//div[@class='brs_col']/p/a")
.Attributes["href"]
.Value;
回答2:
You can try this
doc.DocumentNode.SelectNodes("//a[@href]").FirstOrDefault();
回答3:
This if you sure that is the first url in the whole HTML document:
doc.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;
Or this if you sure that is the first ulr in the class brs_col
doc.DocumentNode.SelectSingleNode("//div[@class='brs_col']//a").Attributes["href"].Value;
来源:https://stackoverflow.com/questions/16662938/c-sharp-htmlagilitypack-parse-first-link-from-div-with-class