使用BeautifulSoup的find()方法来获取子元素。下面是一个示例代码:
from bs4 import BeautifulSoup
html = '''
Parent element
Child element
'''
soup = BeautifulSoup(html, 'html.parser')
parent_element = soup.find(id='parent')
child_element = parent_element.find(id='child')
print(child_element)
运行上述代码,将输出子元素的内容:
Child element
使用find()方法可以根据指定的条件查找子元素,如id、标签名等。