C#htmlagilitypack解析div与类的第一个链接? [英] c# htmlagilitypack parse first link from div with class?
本文介绍了C#htmlagilitypack解析div与类的第一个链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析/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>
我尝试使用以下XPATH选择它,但似乎无法使它们中的任何一个起作用:
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]
有什么想法吗?
推荐答案
如果您确定这是整个HTML文档中的第一个网址,则此操作:
This if you sure that is the first url in the whole HTML document:
doc.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;
或者,如果您确定这是brs_col类中的第一个ulr,则此
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;
这篇关于C#htmlagilitypack解析div与类的第一个链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文