How to Find HTML Elements of a Class or an ID in Python Using BeautifulSoup



Python


In this article, we show how to find HTML elements of a certain class or a certain ID in Python using BeautifulSoup.

So let's say that we have a paragraph that has a class attribute that is equal to "topsection".

How can we get all paragraph tags that have a class that is equal to "topsection"

And the way we do this is by using a dictionary with a key of class and a value equal to "top".

This is shown in the code below.



So let's go over this code now.

So we import the requests module.

We then import BeautifulSoup from bs4.

We then create a variable called getpage and set it equal to requests.get('http://www.learningaboutelectronics.com')

We then create the BeautifulSoup version of this page and parse the HTML elements of this document.

We then create a variable called all_class_topsection. This finds all p elements (paragraph elements) that have a class attribute equal to "topsection"

We then create a for loop that loops through all paragraph tags of the class "topsection" and prints each one.

And id is the same exact thing.

Let's say we have paragraphs with an id equal to "para1"

The code to print out all paragraph tags with an id of "para1" is shown below.



So this code retreives and prints out all paragraph elements with an id of "para1"

And this is how you can access element with specific class or id attributes in Python using BeautifulSoup.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...