可以使用recursive参数来遍历所有嵌套标签,以获取所需的内容。例如:
from bs4 import BeautifulSoup
html = 'OuterInner'
soup = BeautifulSoup(html, 'html.parser')
# 使用recursive参数遍历所有标签
for div in soup.find_all('div', recursive=True):
print(div.text)
输出结果为:
Outer
Inner
这样,即可访问所有的嵌套标签。