Wednesday, February 18, 2026
HomeLanguagesGet a list of all the heading tags using BeautifulSoup

Get a list of all the heading tags using BeautifulSoup

In order to print all the heading tags using BeautifulSoup, we use the find_all() method. The find_all method is one of the most common methods in BeautifulSoup. It looks through a tag and retrieves all the occurrences of that tag.

Syntax: find_all(name, attrs, recursive, string, limit, **kwargs)

An HTML document consists of the following tags – h1, h2, h3, h4, h5, and h6. The most commonly used HTML tags in webpages are h1, h2 and h3, and to find these we pass a list of tags as an argument to the find_all() method.

Steps:

  1. import the libraries requests and BeautifulSoup
  2. pass a URL into a variable
  3. use the request library to fetch the URL
  4. create a BeautifulSoup object
  5. create a list of heading tags ()
  6. iterate over all the heading tags using find_all() method

Example:

Python3




# Python program to print all heading tags
import requests
from bs4 import BeautifulSoup
 
# scraping a wikipedia article
request = requests.get(url_link)
 
Soup = BeautifulSoup(request.text, 'lxml')
 
# creating a list of all common heading tags
heading_tags = ["h1", "h2", "h3"]
for tags in Soup.find_all(heading_tags):
    print(tags.name + ' -> ' + tags.text.strip())


Output: 

h2 -> Related Articles
h2 -> Python3
h2 -> Python3
h2 -> Python3
h2 -> Python3
h2 -> Python3
h2 -> Python3
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS