使用 find_all() 方法来获取所有符合要求的 div 标签,然后使用其它方法来处理这些标签。例如:
html_doc = """
The Dormouse's story
Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.
Paragraph 1
Paragraph 2
Paragraph 3
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
div_list = soup.find_all('div', {'class': 'section'})
for div in div_list: h2 = div.find('h2') print(h2.text) p_list = div.find_all('p') for p in p_list: print(p.text)
输出结果:
Section 1 Paragraph 1
Section 2 Paragraph 2 Paragraph 3
在上面的例子中,使用 find_all() 方法找到了所有带有 class="section" 的 div 标签,然后遍历这些标签,提取其中的 h2 和 p 标签的值。