【发布时间】:2018-04-18 10:42:17
【问题描述】:
我编写了一个 xpath 表达式来从一些 html elements 中获取 page number 的最大值。但是,使用下面的 xpath,我得到了最后一个文本,在这种情况下是 Next Page。我希望我的 xpath 以这样的方式运行,以便我可以获得最高的数字,就像在 6 中使用它一样。
应应用 xpath 的元素:
content = """
<div class="nav-links"><span aria-current="page" class="page-numbers current"><span class="meta-nav screen-reader-text">Page </span>1</span>
<a class="page-numbers" href="https://page/2/"><span class="meta-nav screen-reader-text">Page </span>2</a>
<span class="page-numbers dots">…</span>
<a class="page-numbers" href="https://page/6/"><span class="meta-nav screen-reader-text">Page </span>6</a>
<a class="next page-numbers" href="https://page/2/"><span class="screen-reader-text">Next Page</span></a></div>
"""
到目前为止我已经尝试过:
from lxml.html import fromstring
root = fromstring(above_content)
pagenum = root.xpath("//*[contains(@class,'page-numbers')][last()]/span")[0].text
print(pagenum)
我的输出:
Next Page
我希望得到的输出:
6
【问题讨论】:
标签: python python-3.x xpath lxml