How to find elements by class using BeautifulSoup
In this tutorial, we are going to know how to find elements by class using BeautifulSoup. Finding elements in a class is done in two ways, either by knowing the class name or by the class name and tag name.
Importing the modules required:-
bs4 module:- From this module, we will use a library called BeautifulSoup for fetching the data from a webpage or xml Document, or html document.
from bs4 import BeautifulSoup
Find elements by class using BeautifulSoup in Python
First, we will look at how to find by a class name, In the BeautifulSoup library, we have a method called find_all()
which takes a class name as a parameter and gives us all the elements which are under that class name.
Finding the element by knowing Class name
from bs4 import BeautifulSoup #An Example HTML source code to find element in class Source=""" <div> <p class="Case-1">Welcome to the tutorial</p> <p class="Case-1">This is Chaithanya</p> <h class="Case-1">Have a good day</h> <p class="Case-2">Morning Sunshine</p> <p class="Case-2">Be Good</p> <p class="Case-3">Thank you</p> <p class="Case-4">Bye bye...</p> </div>""" #Converting the HTML content to a Soup object html_soup_object = BeautifulSoup(Source, 'html.parser') #Finding all the elements by using class name called "Case-1" find_by_class = html_soup_object.find_all(class_="Case-1") if len(find_by_class) == 0: print("No Elements are found under this class name") else: print(find_by_class,sep="\n")
output:-
[<p class="Case-1">Welcome to the tutorial</p>, <p class="Case-1">This is Chaithanya</p>, <h class="Case-1">Have a good day</h>]
we got all the elements of the class named “Case-1”.
If we want to print the content of the elements then follow these lines of code
Content = html_soup_object.find_all(class_="Case-1") for element in Content: print(element.string)
output :-
Welcome to the tutorial This is Chaithanya Have a good day
Finding the element by knowing the Class name and tag name:-
Now we will find elements by class name and tag name and this way is accurate compared to our last method. In the above method, it gives us all the elements present under the given class name irrespective of the tag of the element. So by using the tag name of the element we can get the elements of that class under the given tag name.
For our find_all() method we can send a tag name as parameter, So it fetches the data under that tag name;
Syntax :-
find_element = html_soup_object.find_all('p', class_="Case-1")
We’ll see the difference by an example,
from bs4 import BeautifulSoup #Example of HTML source code to find element in class Source=""" <div> <p class="Case-1">Welcome to the tutorial</p> <p class="Case-1">This is Chaithanya</p> <h class="Case-1">Have a good day</h> <p class="Case-2">Morning Sunshine</p> <p class="Case-2">Be Good</p> <p class="Case-3">Thank you</p> <p class="Case-4">Bye bye...</p> </div>""" #Converting the HTML content to a Soup object html_soup_object = BeautifulSoup(Source, 'html.parser') #Finding all the elements by using class name and tag name find_element = html_soup_object.find_all('p', class_="Case-1") print(find_element)
output:-
[<p class="Case-1">Welcome to the tutorial</p>, <p class="Case-1">This is Chaithanya</p>]
See When we are searching by class name we are getting all elements irrespective of their tags(All elements of class “Case-1” such as ‘p’, ‘h’, etc.). But when we give the required tag name Only the elements under that tag and class name are shown.
Also read: How to use Xpath with BeautifulSoup with an Example
Amazing, Mind blowing, Unbelievable… Helped me alot . Thank you Mr. Chaitanya Pranav Sai