python - 如何使用BeautifulSoup访问标签的属性值

标签 python web-scraping beautifulsoup

我正在使用 BeautifulSoup 并请求进行网页抓取。我知道如何提取标签之间的属性,但如果我想要的是标签中下面的数字 '4.31',知道如何获取它吗?

<div class="starRating" title="4.31">
<svg
 ...
</svg>
</div>

我已经尝试过:

soup.find('div',{'class':'starRating'})
soup.find('title')

它什么也不返回,所以数字基本上就是标签......

最佳答案

您可以读取属性title像这样的值:

from bs4 import BeautifulSoup


response = """
<html>
<div class="starRating" title="4.31">
<svg>
</svg>
</div>
</html>
"""

soup = BeautifulSoup(response, 'lxml')
print(soup.find('div', {'class': 'starRating'})['title'])

输出:

4.31

参见https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes `

A tag may have any number of attributes. The tag <b id="boldest"> has an attribute “id” whose value is “boldest”. You can access a tag’s attributes by treating the tag like a dictionary

关于python - 如何使用BeautifulSoup访问标签的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59514753/

相关文章:

python - 为什么当我点击 Dictionary 变量时 Spyder 不能打开它?

python - Selenium pdf自动下载不起作用

python - ImportError 没有名为 BeautifulSoup 的模块 - 路径错误

python - 在 Python 中查找 beautifulsoup 的链接

python - 抓取的网站数据未写入 CSV

python - 通过迭代邻接矩阵使用公式计算 PageRank

sql查询中的python列表作为参数

python - 如何理解tensorflow错误消息?

javascript - 为什么 CasperJS 用 [object Object] 填充文本输入

python - Beautifulsoup 解析错误