使用BeautifulSoup库的find_all方法,可以通过传递一个字典参数来检查多个属性中是否存在某个属性。示例如下:
from bs4 import BeautifulSoup
html = '''
Red text
Blue text
Green text
Red and blue text
'''
soup = BeautifulSoup(html, 'html.parser')
# 使用find_all检查多个属性中是否存在某个属性
elements = soup.find_all('p', {'class': ['red', 'blue']})
# 打印匹配到的元素
for element in elements:
print(element.text)
在上面的示例中,我们使用find_all方法来查找所有的标签,同时检查它们的
class
属性是否包含red
和blue
。输出结果将是匹配到的元素的文本内容:
Red text
Red and blue text