Find tags by CSS class using BeautifulSoup
Last Updated :
12 Jan, 2024
BeautifulSoup is a Python library for pulling data out of HTML and XML files. When working with HTML documents, we often need to extract specific elements based on their CSS classes. In this article, we will discuss how to find tags by CSS using BeautifulSoup. We are given an HTML document, we need to find and extract tags from the document using the CSS class.
Examples
HTML Document:
<html>
<head>
<title> Geeksforgeeks </title>
</head>
<body>
<div class="ext" >Extract this tag</div>
</body>
</html>
Output:
<div class="ext" >Extract this tag</div>
Required Modules
bs4: It is a Python library used to scrape data from HTML, XML, and other markup languages.
Make sure you have pip installed on your system. Run the following command in the terminal to install this library-
pip install bs4
or
pip install beautifulsoup4
Find Tags by CSS Class Using BeautifulSoup
Below are the ways by which we can find tags using BeautifulSoup in Python:
- Using find() method
- Using find_all() function
- Using Regular Expressions
- Using the User-Defined function
- From a Website
Example 1: Find the Tag Using find() Method
In this example, a function find_tags_from_class
is defined to parse an HTML document using BeautifulSoup and extract a specific <div>
tag with a given CSS class. The function is called with a sample HTML string to demonstrate the extraction process.
Python3
from bs4 import BeautifulSoup
HTML_DOC =
def find_tags_from_class(html):
soup = BeautifulSoup(html, "html.parser" )
div = soup.find( "div" , class_ = "ext" )
print (div)
find_tags_from_class(HTML_DOC)
|
Output:

Example 2: Find All the Tags Using find_all() Method
In this example, a function find_tags_from_class
is defined to parse an HTML table using BeautifulSoup and extract all <td>
tags with a specific CSS class “table-row”. The function iterates through the extracted tags and prints each row to the console.
Python3
from bs4 import BeautifulSoup
HTML_DOC =
def find_tags_from_class(html):
soup = BeautifulSoup(html, "html.parser" )
rows = soup.find_all( "td" , class_ = "table-row" )
for row in rows:
print (row)
find_tags_from_class(HTML_DOC)
|
Output

Example 3: Finding Tags by CSS class Using Regular Expressions
In this example, a function find_tags_from_class
is defined to parse an HTML table using BeautifulSoup and extract all <td>
tags whose CSS class names end with “row” using a regular expression pattern. The function iterates through the extracted tags and prints each row to the console.
Python3
from bs4 import BeautifulSoup
import re
HTML_DOC =
def find_tags_from_class(html):
soup = BeautifulSoup(html, "html.parser" )
rows = soup.find_all( "td" , class_ = re. compile ( "row$" ))
for row in rows:
print (row)
find_tags_from_class(HTML_DOC)
|
Output

Explanation
<td class="table-row"> This is row 2 </td>
<td class="table-row"> This is row 4 </td>
Above two tags class name ends with “row”. Therefore, they are extracted. Other tags class name doesn’t end with “row”. Therefore, they are not extracted.
Example 4: Finding Tags by CSS Class Using the User-Defined Function
In this example, a user-defined function has_three_characters
is defined to check if a CSS class name is not None and has a length of 3 characters. The function find_tags_from_class
parses an HTML table using BeautifulSoup and extracts all <td>
tags based on the condition specified by the user-defined function. The function then iterates through the extracted tags and prints each valid row to the console.
Python3
from bs4 import BeautifulSoup
HTML_DOC =
def has_three_characters(css_class):
return css_class is not None and len (css_class) = = 3
def find_tags_from_class(html):
soup = BeautifulSoup(html, "html.parser" )
rows = soup.find_all( "td" , class_ = has_three_characters)
for row in rows:
print (row)
find_tags_from_class(HTML_DOC)
|
Output:

Example 5: Finding Tags by CSS Class from a Website
In this example, the requests
module is used to fetch the HTML content of the GeeksforGeeks website. The function find_tags_from_class
parses the fetched HTML content using BeautifulSoup with the ‘html5lib’ parser and extracts a specific <div>
tag with the CSS class “article–container_content”. The extracted tag is then printed to the console.
Python3
from bs4 import BeautifulSoup
import requests
import requests
HTML_DOC = requests.get(URL)
def find_tags_from_class(html):
soup = BeautifulSoup(html.content, "html5lib" )
div = soup.find( "div" , class_ = "article--container_content" )
print (div)
find_tags_from_class(HTML_DOC)
|
Output:

Similar Reads
BeautifulSoup - Find tags by CSS class with CSS Selectors
Prerequisites: Beautifulsoup Beautifulsoup is a Python library used for web scraping. BeautifulSoup object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The
2 min read
Get data inside a button tag using BeautifulSoup
Sometimes while working with BeautifulSoup, are you stuck at the point where you have to get data inside a button tag? Don't worry. Just read the article and get to know how you can do the same. For instance, consider this simple page source having a button tag. C/C++ Code <!DOCTYPE html> <
2 min read
Python BeautifulSoup - find all class
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: bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This modu
2 min read
Find the siblings of tags using BeautifulSoup
Prerequisite: BeautifulSoup BeautifulSoup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come in built-in with Python. To install this type the below command in the terminal. In this article, we will learn about siblings in HTML tags using BeautifulSoup. He
2 min read
How to Scrape Nested Tags using BeautifulSoup?
We can scrap the Nested tag in beautiful soup with help of. (dot) operator. After creating a soup of the page if we want to navigate nested tag then with the help of. we can do it. For scraping Nested Tag using Beautifulsoup follow the below-mentioned steps. Step-by-step Approach Step 1: The first s
3 min read
Get tag name using Beautifulsoup in Python
Prerequisite: Beautifulsoup Installation Name property is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. Name object corresponds to the name of an XML or HTML t
1 min read
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
2 min read
How to modify HTML using BeautifulSoup ?
BeautifulSoup in Python helps in scraping the information from web pages made of HTML or XML. Not only it involves scraping data but also involves searching, modifying, and iterating the parse tree. In this article, we will discuss modifying the content directly on the HTML web page using BeautifulS
3 min read
BeautifulSoup CSS selector - Selecting nth child
In this article, we will see how beautifulsoup can be employed to select nth-child. For this, select() methods of the module are used. The select() method uses the SoupSieve package to use the CSS selector against the parsed document. Syntax: nth-child() SelectorSyntax: select("css_selector") CSS SE
3 min read
Show text inside the tags using BeautifulSoup
Prerequisite: RequestsBeautifulSoup In this article, we will learn how to get a text from HTML tags using BeautifulSoup. Here we will use requests & BeautifulSoup Module in Python. The requests library is an integral part of Python for making HTTP requests to a specified URL. Whether it be REST
2 min read