python - how to get the penultimate element? -


please using xpath content of second tag . below code wrote. not work

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() , @rel != "next"]/text()')  print(res) 

you current expression not work because a[position() = last() , @rel != "next"] tries match last element if rel attribute different "next". not case in markup, expression matches nothing.

you can compare position() against last() - 1 instead:

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

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -