BeautifulSoup: .children Example
Syntax
element.children
.children example
from bs4 import BeautifulSoup
# Html source
html_source = '''
<div>
<h1>child 1</h1>
<h1>child 2</h1>
<h1>child 3</h1>
</div>
'''
# Parsing
soup = BeautifulSoup(html_source, 'html.parser')
# find <div> tag
el = soup.find('div')
# Print <div> children
for child in el.children:
print(child)
Output:
<h1>child 1</h1>
<h1>child 2</h1>
<h1>child 3</h1>