简介
select_one 是查找并返回符合条件的第1个元素。
select 是查找并返回所有符合条件的元素。
示例
代码:
from bs4 import BeautifulSoup
html_content = '''
<div>测试01</div>
<div>测试02</div>
'''
soup = BeautifulSoup(html_content, 'html.parser')
print('--------- select ---------')
for ele in soup.select('div'):
print(ele)
print('--------- select_one ---------')
print(soup.select_one('div'))
执行结果:
--------- select ---------
<div>测试01</div>
<div>测试02</div>
--------- select_one ---------
<div>测试01</div>