示例
from bs4 import BeautifulSoup
html_content = '''
<div id="content" data="你好">
<p>测试01</p>
<span>测试02</span>
</div>
<div>测试03</div>
'''
soup = BeautifulSoup(html_content, 'html.parser')
content_div = soup.select_one("#content")
p_in_content_div = content_div.select('p')
print('--------- content_div ---------')
print(content_div)
print('------ p_in_content_div -------')
print(p_in_content_div)
执行结果:
--------- content_div ---------
<div data="你好" id="content">
<p>测试01</p>
<span>测试02</span>
</div>
------ p_in_content_div -------
[<p>测试01</p>]