Beautifulsoup: Find h1 Element

Find h1 Element

Syntax:

soup.h1

Example:

from bs4 import BeautifulSoup

html = """
<header>

<h1>Beautifulsoup: Find all stylesheet links</h1>
<h1>Beautifulsoup: Find all H1 element</h1>
</header>

<div class="content">
<h2>Syntax</h2>
<pre><code class="language-python">soup.find_all('link', rel="stylesheet")</code></pre>
<h2>Find all stylesheet links example</h2>
"""

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


print(soup.h1)

Output:

#<h1>Beautifulsoup: Find all stylesheet links</h1>

Find all H1 elements

Syntax:

soup.find_all('h1')

Example:

from bs4 import BeautifulSoup

html = """
<header>

<h1>Beautifulsoup: Find all stylesheet links</h1>
<h1>Beautifulsoup: Find all H1 element</h1>
</header>

<div class="content">
<h2>Syntax</h2>
<pre><code class="language-python">soup.find_all('link', rel="stylesheet")</code></pre>
<h2>Find all stylesheet links example</h2>
"""

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


#Find
allh1 = soup.find_all('h1')

#print
print(allh1)

Output:

[<h1>Beautifulsoup: Find all stylesheet links</h1>, <h1>Beautifulsoup: Find all H1 element</h1>]