How to Find Elements by CSS selector in Beautifulsoup

In this tutorial, we are going to see how to find elements by CSS selector in BeautifulSoup. BeautifulSoup has only some CSS selectors, Now we use two methods to find elements select( ) to find multiple elements and select_one( ) to find the single element.

If you want to see how to add  .html file or html url instead of putting the HTML source code, Refer this Example reference for how to add a .html file or html URL in that we have mentioned the process for lxml parser and we presented by putting .html file and also html URL, In a similar way implement the process in our code.

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

 

How to Find Elements by CSS selector in BeautifulSoup.

First, we will see how to find a single element by using the select_one( ) method in BeautifulSoup.

Finding single element :-

from bs4 import BeautifulSoup
# html source code
Source_html = """
<head>
<body>
<div id = "myclass">
<h1>I am roll number 1</h1>
<h2>I am roll number 2</h2>
<h3>I am roll number 3</h3>
</div>"""
# Soup object of html content
Soup_obj = BeautifulSoup(Source_html, 'html.parser')
# Find all the element CSS selector
elements = Soup_obj.select_one("div>*")
print(elements) # To know the element
print(elements.string) # To know the content of the element

The output will be,

Output :-

<h1>I am roll number 1</h1>
I am roll number 1

Finding Multiple elements :-

we will see how to find a multiple elements by using the select( ) method in BeautifulSoup.

from bs4 import BeautifulSoup
# html source code
Source_html = """
<head>
<body>
<div id = "myclass">
<h1>I am roll number 1</h1>
<h2>I am roll number 2</h2>
<h3>I am roll number 3</h3>
</div>"""
# Soup object of html content
Soup_obj = BeautifulSoup(Source_html, 'html.parser')
# Find all the elements CSS selector
elements = Soup_obj.select("div>*")
print(elements) # prints all elements under that tag
#To find the content of the element
for ele in elements:
    print(ele.string)

 

So, we will get all the elements under the ‘div’ tag.

Output :-

[<h1>I am roll number 1</h1>, <h2>I am roll number 2</h2>, <h3>I am roll number 3</h3>]
I am roll number 1
I am roll number 2
I am roll number 3

Now we will see how to find elements by id.

Finding by Id attributes :-

The character # is used in CSS to select tags by their id attributes.

from bs4 import BeautifulSoup
# html source code
Source_html = """
<head>
<body>
<div id = "myclass">
<h1>I am roll number 1</h1>
<h2>I am roll number 2</h2>
<h3>I am roll number 3</h3>
</div>"""
# Soup object of html content
Soup_obj = BeautifulSoup(Source_html, 'html.parser')
# Find all the elements CSS selector with id attribute
elements = Soup_obj.select("#myclass>*")
print(elements) # prints all elements under the id

Here all the elements under the id “myclass” will be our output.

Output :-

[<h1>I am roll number 1</h1>, <h2>I am roll number 2</h2>, <h3>I am roll number 3</h3>]

Leave a Reply

Your email address will not be published.