Some text here
可能是类名称书写错误,可以检查一下HTML源代码,或者尝试使用其他CSS选择器。
示例:
假设有以下HTML代码:
Welcome!
Some text here
假设我们想获取class为'content”的div元素。我们可以使用以下代码:
from bs4 import BeautifulSoup
html_doc = """
Welcome!
Some text here
"""
soup = BeautifulSoup(html_doc, 'html.parser')
content_div = soup.find('div', class_='content')
print(content_div)
输出结果应该是这样的:
Some text here
注意,在使用class属性来查找元素时,我们需要使用'class_”而不是'class”。这是因为Python中'class”是一个保留关键字,所以我们使用'class_”来代替。