First paragraph.
Second paragraph.
检查所查找的元素是否存在,或者尝试使用其他方法查找相关元素。
示例代码:
from bs4 import BeautifulSoup
html = """
Beautifulsoup Test
Example
First paragraph.
Second paragraph.
"""
soup = BeautifulSoup(html, 'html.parser')
result = soup.find_all('p', class_='content2')
print(result)
运行上述代码会输出一个空列表,因为class名为'content2'的元素不存在于html中。为了解决这个问题,可以先用find方法查找是否存在要查找的元素,或者尝试使用其他方法查找相关元素。