请帮助使用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)
答案 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()