1、简介
find 是查找并返回符合条件的第1个元素。 find_all 是查找并返回所有符合条件的元素。
2、示例
2.1、示例:使用 name 指定标签名
代码:
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<div>测试02</div>
<h1>测试H1</h1>
'''
soup = BeautifulSoup(html_content, 'html.parser')
print('--------- find_all ---------')
for ele in soup.find_all(name='div'):
print(ele)
print('--------- find ---------')
print(soup.find(name='div'))
执行结果:
--------- find_all ---------
<div>测试01</div>
<div>测试02</div>
--------- find ---------
<div>测试01</div>
2.2、示例:直接指定标签名
代码:
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<div>测试02</div>
<h1>测试H1</h1>
'''
soup = BeautifulSoup(html_content, 'html.parser')
print('--------- find_all ---------')
for ele in soup.find_all('div'):
print(ele)
print('--------- find ---------')
print(soup.find('div'))
执行结果:
--------- find_all ---------
<div>测试01</div>
<div>测试02</div>
--------- find ---------
<div>测试01</div>
2.3、示例:指定多个标签
代码:
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<div>测试02</div>
<h1>测试H1</h1>
'''
soup = BeautifulSoup(html_content, 'html.parser')
print('--------- find_all ---------')
for ele in soup.find_all(['h1', 'h2', 'div']):
print(ele)
print('--------- find ---------')
print(soup.find(['h1', 'h2', 'div']))
执行结果:
--------- find_all ---------
<div>测试01</div>
<div>测试02</div>
<h1>测试H1</h1>
--------- find ---------
<div>测试01</div>