Last Updated : 23 Jul, 2025
Prerequisite:- Requests , BeautifulSoup
The task is to write a program to find all the classes for a given Website URL. In Beautiful Soup there is no in-built method to find all classes.
Module needed:
pip install bs4
pip install requests
Methods #1: Finding the class in a given HTML document.
Approach:
Code:
Python3
# html code
html_doc = """<html><head><title>Welcome to geeksforgeeks</title></head>
<body>
<p class="title"><b>Geeks</b></p>
<p class="body">geeksforgeeks a computer science portal for geeks
</body>
"""
# import module
from bs4 import BeautifulSoup
# parse html content
soup = BeautifulSoup( html_doc , 'html.parser')
# Finding by class name
soup.find( class_ = "body" )
Output:
<p class="body">geeksforgeeks a computer science portal for geeks </p>
Methods #2: Below is the program to find all class in a URL.
Approach:
Code:
Python3
# Import Module
from bs4 import BeautifulSoup
import requests
# Website URL
URL = 'https://www.geeksforgeeks.org/'
# class list set
class_list = set()
# Page content from Website URL
page = requests.get( URL )
# parse html content
soup = BeautifulSoup( page.content , 'html.parser')
# get all tags
tags = {tag.name for tag in soup.find_all()}
# iterate all tags
for tag in tags:
# find all element of tag
for i in soup.find_all( tag ):
# if tag has attribute of class
if i.has_attr( "class" ):
if len( i['class'] ) != 0:
class_list.add(" ".join( i['class']))
print( class_list )
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4