How to find element by class using Beautifulsoup

A step-by-step guide on how to find elements by class using Beautifulsoup.

Important: we will use real-life example in this tutorial, so you will need requests and Beautifulsoup libraries installed.

Step 1. Let’s start by importing the Beautifulsoup library.

from bs4 import BeautifulSoup

Step 2. Then, import requests library.

import requests

Step 3. Get a source code of your target landing page. We will be looking for guide titles on our homepage in this example.


A universal code looks like this:

r=requests.get("Your URL")

Step 4. Convert HTML code into a Beautifulsoup object named soup.


Step 5.  Inspect the page to find a class you would like to extract.

How to find element by class using Beautifulsoup

The code of this class looks like this:

elements_by_class = soup.find_all(class_ = "archive-list__title")

NOTE: Since we want to find all the titles instead of one, we use soup.find_all() instead of soup.find().

Step 6. Let’s check if the script works by printing its output into the terminal window.



NOTE: If you want to display only the titles, you can get the string attribute of each scraped element.

for element in elements_by_class: print (element.string)

for element in elements_by_class: print (element.string


Congratulations, you’ve found and extracted the content of class using Beautifulsoup. Here’s the full script:

from bs4 import BeautifulSoup
import requests
r = requests.get("")
soup = BeautifulSoup(r.content, "html.parser")
elements_by_class = soup.find_all(class_ = "archive-list__title")


for element in elements_by_class:
    print (element.string)


Best value provider
Success rate: 98.69%
Response time: 1.29s

Best premium provider
Success rate: 99.57%
Response time: 0.86s

Most customizable provider
Success rate: 99.47%
Response time: 1.08s

Submit a comment

Your email address will not be published.

Rate this post