python - 从 Python 中的 html 代码 href 解析页面
问题描述
我有一个 html,其中包含以下代码:
<a href="?page=5" title="Go to last page">
<span class="visually-hidden">Last page</span>
<span aria-hidden="true">Last »</span>
</a>
5
有人可以帮助我使用 Beautiful Soup获取最后一个页码(在这种情况下)吗?谢谢
解决方案
您可以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)
推荐阅读
- cassandra - READ 消息在最后 5000 毫秒内被丢弃:2 个内部和 0 个跨节点。平均内部丢弃延迟:2556 毫秒
- python - 读取 JSON 文件并将 JSON 对象附加到 JSON 数组
- angular - 如何从 Angular 应用程序使用 Google Cloud Speech-to-Text
- list - 节省空间的功能符号书写
- python - 使用 Python GDAL 库将矢量 (.shp) 转换为光栅 (.tiff)
- javascript - CSS Carousel 在 Gridsome 应用程序中不起作用?
- angular - 如何在 Firefox 浏览器中打开检查元素
- reactjs - 如何写入控制台属性数据?
- laravel - 请求在 https 上工作,但由于 cors 而在 http 上被阻止?
- mysql - 如何避免 SQL 中新添加的列标记出现空值?