我有一个 html,其中包含以下代码:
<a href="?page=5" title="Go to last page">
<span class="visually-hidden">Last page</span>
<span aria-hidden="true">Last »</span>
</a>
有人可以帮我使用 Beautiful Soup 获取最后页码(在本例中为 5
)吗?
谢谢
最佳答案
您可以使用str.split
上href=
属性:
last_page = soup.find('a')['href'].split('=')[-1]
print(last_page)
打印:
5
或者(使用 <a>
选择 title="Go to last page"
标签:
last_page = soup.find('a', title='Go to last page')['href'].split('=')[-1]
print(last_page)
关于python - 在Python中从html代码href解析页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64572500/