如何获得倒数第二个元素?

时间:2014-03-10 10:47:46

标签: python xpath lxml

请帮助使用xpath获取第二个标记的内容。下面是我写的代码。但它不起作用

import lxml.html

doc = lxml.html.document_fromstring("""
    <nav class="Paging">
            <a href="/women/dresses/cat/4?page=1" class="active">1</a>
            <a href="/women/dresses/cat/4?page=2">2</a>
            <a href="/women/dresses/cat/4?page=2" rel="next">Next »</a>
    </nav>
""")
res = doc.xpath('//nav[@class="Paging"][position() = 1]/a[position() = last() and @rel != "next"]/text()')

print(res)

2 个答案:

答案 0 :(得分:0)

您当前的表达式不起作用,因为a[position() = last() and @rel != "next"]尝试匹配最后一个元素,如果rel属性与"next"不同。标记中的情况并非如此,因此表达式不匹配。

您只需将position()last() - 1进行比较即可:

res = doc.xpath('//nav[@class = "Paging" and position() = 1]'
    + '/a[position() = last() - 1]/text()')

答案 1 :(得分:0)

你可以试试xpath:

//nav[@class="Paging"][position() = 1]/a[position() = last() - 1][not(@rel)]/text()