这是第一段文本。
这是第二段文本。
这是第三段文本。
使用find_all()方法来获取所有匹配的元素,然后进行遍历或使用索引来访问需要的元素。
示例代码:
from bs4 import BeautifulSoup
html = """
这是第一段文本。
这是第二段文本。
这是第三段文本。
soup = BeautifulSoup(html, 'html.parser') content = soup.find_all('div', {'class': 'content'}) for p in content[0].find_all('p'): print(p.text) #输出每个p标签的文本内容