首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用优美汤获取特定属性

使用优美汤获取特定属性
EN

Stack Overflow用户
提问于 2019-01-25 06:53:13
回答 1查看 7.5K关注 0票数 4

我想从HTML标签中使用漂亮汤提取属性。怎么做?

例如:

代码语言:javascript
运行
复制
<div class="search-pagination-top clearfix  mtop ">
                                            <div class="row"><div class="col-l-4 mtop pagination-number" tabindex="0"
aria-label="Page 1 of 15 "><div>Page <b>1</b> of <b>15</b> </div></div>

如何从“咏叹号”属性中获取文本?

我试着使用select(),但是没有用。

EN

回答 1

Stack Overflow用户

发布于 2019-01-25 07:00:06

您可以像字典一样提取属性值。使用密钥aria-label

Ex:

代码语言:javascript
运行
复制
from bs4 import BeautifulSoup

html = """<div class="search-pagination-top clearfix  mtop ">
                                            <div class="row"><div class="col-l-4 mtop pagination-number" tabindex="0"
aria-label="Page 1 of 15 "><div>Page <b>1</b> of <b>15</b> </div></div>
"""

soup = BeautifulSoup(html, "html.parser")
print( soup.find("div", class_="col-l-4 mtop pagination-number")["aria-label"] )

输出:

代码语言:javascript
运行
复制
Page 1 of 15 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54360308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档