我正在使用 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/