请确保在BeautifulSoup的find_all方法中正确使用class_参数,并将其设置为字符串形式的类名。例如,要查找类名为“my-class”的div元素,可以使用以下代码:
from bs4 import BeautifulSoup
html = 'Hello, world!'
soup = BeautifulSoup(html, 'html.parser')
# 使用class_参数查找div元素
divs = soup.find_all('div', class_='my-class')
# 输出查找结果
print(divs)
输出结果应为:
[Hello, world!]
注意:在class参数后面加一个下划线“_”,是因为“class”在Python中是一个保留关键字,不能直接作为参数名。