要使用Beautiful Soup获取包含精确指定类的标签,你可以使用find_all
方法,并将class_
参数设置为要查找的类名。下面是一个示例代码:
from bs4 import BeautifulSoup
html = '''
Beautiful Soup - 获取包含精确指定类的标签
Title
This is a description.
This is another paragraph.
'''
soup = BeautifulSoup(html, 'html.parser')
tags = soup.find_all(class_="description")
for tag in tags:
print(tag)
运行上述代码,你将得到以下输出:
This is a description.
这是因为我们使用find_all
方法查找class
属性值为"description"
的标签,并使用for
循环打印了找到的标签。