Regex 如何在标签中找到最后一个数字
为了使用python传递网站的分页,我想查找网站的最后一页。 我通过firebug检查页面的元素Regex 如何在标签中找到最后一个数字,regex,python-2.7,pattern-matching,Regex,Python 2.7,Pattern Matching,为了使用python传递网站的分页,我想查找网站的最后一页。 我通过firebug检查页面的元素 <div class="pagination"> <ul> <li> <a> <img src="/images" class="pagin
<div class="pagination">
<ul>
<li>
<a>
<img src="/images" class="pagination-l">
</a>
</li>
<li style="float:left;">
<a id="toPage" title="A" class="page-number actif" href="/d">
1
</a>
</li>
<li style="float:left;">
<a id="toPage_0" title="A2" class="p" href="/ew">
2
</a>
</li>
<li style="float:left;">
<a id="toPage_1" title="A3" class="p3" href="/t3">
3
</a>
</li>
<li style="float:left;">...</li>
<li style="float:left;">
<a id="toPage_2" title="t" class="p" href="t2">
5
</a>
</li>
<li>
<a id="nextPage" title="next(2)" href="/test1">
<img src="test.png" class="pagination-r">
</a>
</li>
</ul>
</div>
-
-
-
-
-
这里我想要的数字是5使用解析器和正则表达式的组合:
from bs4 import BeautifulSoup
import re
html = your_html_here
soup = BeautifulSoup(html, "html5lib")
links = soup.findAll('a', text=re.compile('^\s*\d+\s*$'))
number = links[-1].string.strip()
print number
# 5
看到一个字:@jan我不知道最后一页的id谢谢你的回答和演示