获取<a> tag inside li in an ol

时间:2016-04-01 18:45:17

标签: parsing xpath html-agility-pack

<dl id="narrow-by-list">
<dt class="odd">Category</dt>
<dd class="odd">
<ol>
<li>
<a href="http://www.shophive.com/apple/mac?cat=11">MacBook Pro</a>
(7)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=12">MacBook Air</a>
(4)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=485">MacBook </a>
(2)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=13">iMac</a>
(12)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=14">Mac Mini</a>
(5)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=15">Mac Pro</a>
(2)
</li>
<li>
<a href="http://www.shophive.com/apple/mac?cat=22">Accessories</a>
(127)
</li>
</ol>
</dd>

//i want to get all the href inside a tag in li which is in ol inside a class named "odd" i have tried everything by combing them into 1 div as well,

var getcatgory = htmlDoc.DocumentNode.SelectNodes(
    @"//d1[@id='narrow-by-list']/div[@class='odd']/ol/li")

i have checked NodeCollection as well,but that is returning null

2 个答案:

答案 0 :(得分:0)

应为//dl(字母l),而不是//d1(数字1)。

答案 1 :(得分:0)

根据您的示例HTML,dl没有子元素div[@class='odd'],而应该是dd[@class='odd']。所以这是一个可能返回目标<a>元素的XPath:

//dl[@id='narrow-by-list']/dd[@class='odd']/ol/li/a

演示:http://www.xpathtester.com/xpath/7e565607576421eac5a6a273af847011