使用 find_all 获取 div 元素
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<p>测试02</p>
<div>测试03</div>
'''
soup = BeautifulSoup(html_content, 'html.parser')
for element in soup.find_all('div'): # 或者 soup.find_all(name='div')
print('元素: ', element)
执行结果:
元素: <div>测试01</div>
元素: <div>测试03</div>
使用 select 获取 div 元素
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<p>测试02</p>
<div>测试03</div>
'''
soup = BeautifulSoup(html_content, 'html.parser')
for element in soup.select('div'):
print('元素: ', element)
执行结果:
元素: <div>测试01</div>
元素: <div>测试03</div>