Beautifulsoup: select_one() Example

select_one() Example


from bs4 import BeautifulSoup

# html source
html = """
<div>
<h1>This is H1</h1>
<h2>This is H2</h2>
<h3>This is H3</h3>
</div>
"""

# BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')

el = soup.select_one('div > *')

print(el)

Output:

<h1>This is H1</h1>