Last modified: Dec 03, 2026

BeautifulSoup: Find h3 Tags

BeautifulSoup is a popular Python library for parsing HTML and XML. It makes web scraping simple and readable.

One common task is finding <h3> tags on a page. These tags often hold section titles, product names, or article subheadings.

This guide shows you how to find <h3> tags step by step. You will see clear examples and their output.

Why Find h3 Tags?

Headings give structure to a web page. The <h3> tag marks a third-level heading.

Scrapers often collect these headings to build outlines or summaries. They also use them to locate data near a title.

If you are new to Python classes and objects, which BeautifulSoup relies on, a good starting point is starting with the basics of Python classes.

Install BeautifulSoup First

You need two packages. One is BeautifulSoup, and the other is a parser like lxml.

Install them with pip in your terminal.


pip install beautifulsoup4 lxml

Now you can import the library and start parsing HTML.

Create a Sample HTML Document

Let us build a small HTML string to work with. It has several <h3> tags.


from bs4 import BeautifulSoup

html = """

Main Title

First Section

Some text here.

Second Section

More text here.

""" soup = BeautifulSoup(html, "lxml")

This creates a BeautifulSoup object named soup. You can now search it.

Find the First h3 Tag

Use the find method to get the first matching tag. It returns a single Tag object.


first_h3 = soup.find("h3")
print(first_h3)
print(first_h3.text)

First Section

First Section

The text attribute gives you the clean heading text. This is useful when you only need the label.

Find All h3 Tags

Use the find_all method to get every <h3> tag. It returns a list-like ResultSet.


all_h3 = soup.find_all("h3")

for tag in all_h3:
    print(tag.text)

First Section
Second Section
Third Section

You can also loop over the tags and read their attributes. Each tag behaves like a small object with its own data.

If you want to understand how these objects are built, see how inheritance works in Python explained for the underlying class design.

Filter h3 Tags by Class

Many pages style specific headings with a class. You can filter by class using the class_ argument.

Note the underscore. It avoids a clash with the Python class keyword.


featured = soup.find("h3", class_="featured")
print(featured.text)

Third Section

You can also pass a list of classes. BeautifulSoup will match tags that contain all of them.

Filter h3 Tags by Attributes

You can search by any attribute using a dictionary. This works well for ids or data attributes.


html2 = '

Introduction

Conclusion

' soup2 = BeautifulSoup(html2, "lxml") tag = soup2.find("h3", attrs={"id": "intro"}) print(tag.text)

Introduction

The attrs parameter keeps your code explicit. It is easy to read and maintain.

Use CSS Selectors to Find h3 Tags

The select method accepts CSS selectors. This is powerful for nested searches.


html3 = """

Inside Div

Outside Div

""" soup3 = BeautifulSoup(html3, "lxml") inside = soup3.select("div.content h3") print([t.text for t in inside])

['Inside Div']

CSS selectors let you target structure. You can combine tags, classes, and parent elements in one string.

To reuse selectors and parsing logic across multiple pages, you may want to wrap them in a class. The article on practical Python class methods examples shows how methods keep scraping code tidy.

Get Text and Clean Whitespace

Heading text often contains extra spaces and newlines. Use the get_text method with strip=True to clean it.


html4 = "

Spaced Heading

" soup4 = BeautifulSoup(html4, "lxml") clean = soup4.find("h3").get_text(strip=True) print(repr(clean))

'Spaced   Heading'

This removes leading and trailing whitespace. It keeps the text readable for reports or storage.

Find h3 Tags Inside a Parent

Sometimes you only want headings inside a specific container. Call find_all on the parent tag instead of the whole document.


html5 = """

News One

News Two

Sports One

""" soup5 = BeautifulSoup(html5, "lxml") section = soup5.find("section", id="news") for h in section.find_all("h3"): print(h.text)

News One
News Two

This scoped search is faster and more accurate. It avoids picking up unrelated headings.

Common Mistakes to Avoid

Do not confuse find with find_all. The first returns one tag, and the second returns a list.

Remember to import BeautifulSoup from bs4. A missing import is a common error for beginners.

Also, always pass a parser name like "lxml" or "html.parser". It avoids warnings and keeps parsing predictable.

If find returns None, your selector did not match. Check the HTML structure before calling .text on the result.

Practical Example: Collect All h3 Headings

Here is a complete script. It finds every <h3> tag and prints a numbered list.


from bs4 import BeautifulSoup

html = """

Setup

Install the tools.

Usage

Run the script.

Results

Read the output.

""" soup = BeautifulSoup(html, "lxml") headings = soup.find_all("h3") for i, h in enumerate(headings, start=1): print(f"{i}. {h.text}")

1. Setup
2. Usage
3. Results

This pattern works for articles, documentation pages, and product listings. It is simple and reliable.

Conclusion

Finding <h3> tags with BeautifulSoup is quick and clear. Use find for the first match and find_all for every match.

Add filters with class_, attrs, or CSS selectors when you need precision. Clean the text with get_text before you store it.

With these small techniques, you can scrape headings from any page and build useful outlines or datasets.