[英]lxml get second last element
I have a bunch of ul
and I want to get the second last li
element in each. 我有一堆ul
,我想得到每个中的第二个最后一个li
元素。 The number of list items is variable so I cannot do something like 列表项的数量是可变的,所以我不能做类似的事情
tree.xpath('//ul/li[3]') # if it had four elements
You can use last()
function : 你可以使用last()
函数 :
tree.xpath('//ul/li[last()-1]')
According to the documentation: 根据文件:
fn:last() as xs:integer
Summary: Returns the context size from the dynamic context. 摘要:从动态上下文返回上下文大小。 (See Section C.2 Dynamic Context ComponentsXP.) If the context item is undefined, an error is raised: [err:XPDY0002]XP. (请参阅第C.2节动态上下文组件XP。)如果上下文项未定义,则会引发错误:[错误:XPDY0002] XP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.