Python,美汤,获取所有类名

标签 python html class beautifulsoup

给定一个 html 代码让我们说:

 <div class="class1">
    <span class="class2">some text</span>
    <span class="class3">some text</span>
    <span class="class4">some text</span>
    </div>

如何检索所有类名?即:['class1','class2','class3','class4']

我试过:

soup.find_all(class_=True)

但它会检索整个标签,然后我需要对字符串做一些正则表达式

最佳答案

您可以 treat each Tag instance found as a dictionary在检索属性时。请注意,class 属性值将是一个列表,因为class 是一个特殊的"multi-valued" attribute。 :

classes = []
for element in soup.find_all(class_=True):
    classes.extend(element["class"])

或者:

classes = [value
           for element in soup.find_all(class_=True)
           for value in element["class"]]

演示:

from bs4 import BeautifulSoup

data = """
<div class="class1">
<span class="class2">some text</span>
<span class="class3">some text</span>
<span class="class4">some text</span>
</div>
"""

soup = BeautifulSoup(data, "html.parser")

classes = [value
           for element in soup.find_all(class_=True)
           for value in element["class"]]

print(classes)

# Returns
# ['class1', 'class2', 'class3', 'class4']

关于Python,美汤,获取所有类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751699/

相关文章:

Swift 类自定义类型属性

c++ - 将所有类实例的列表存储为类中的静态数组

python - 文件中存在非 ASCII 字符 '\x97',但未声明编码

iphone - 为 iPhone 设计 <select> 标签

python - 如何在 Python 中将一个数组中的多个列堆叠在一起?

javascript - 禁用焦点 - jQuery

html - eclipse 中的http 404错误

jQuery 选择具有相同类的随机元素

python - 关系 "background_task"不存在

python - 我的代码在创建这个矩阵时哪里出了问题?