Get all the heading tags from a web page using BeautifulSoup
In this tutorial, we will learn how to get all the heading tags from a web page using BeautifulSoup.
You should have the basic knowledge of how to import requests to load the webpage and import beautifulsoup.
We will extract the data from the website mentioned below:
You can refer to the code below for this purpose:
from bs4 import BeautifulSoup import requests
We will use the below command to get data from the webpage in the form of a string.
requests.get("___").text
page=requests.get("https://www.codespeedy.com/").text soup=BeautifulSoup(page,"html.parser")
Also read: Extract only HTML body texts using beautifulsoup in Python
To get the heading tags:
Now, we will make the list of all the heading tags as shown below:
l=[f'h{i}' for i in range(1,7)]
By printing l, we get the required list:
print(l)
OUTPUT is a list:
Creating a for loop to access the heading tags using find_all():
INPUT:
for head in soup.find_all(l): print(f'{head.name}: {head.text.strip()}')
OUTPUT:
Leave a Reply