Python BeautifulSoup - Get HTML Element by ID
Python BeautifulSoup - Get HTML Element by ID
You can get an HTML element by its ID using Python BeautifulSoup.
To get the HTML element by ID using Python BeautifulSoup, parse the HTML content, and then use BeautifulSoup.find() method with id named argument.
Steps to get HTML element by ID using BeautifulSoup
- Given HTML content in html_content as a string value.
- Parse the HTML content. Call BeautifulSoup() constructor, and pass the HTML content string as argument. The constructor returns a BeautifulSoup object.
soup = BeautifulSoup(html_content, 'html.parser')
- Call find() method on the returned BeautifulSoup object soup, and pass the id named argument. Specify the id value of required HTML element, as value for the id named argument. The find() method returns the HTML element, with the specified id attribute, as Tag object.
element_with_id = soup.find(id="my_div")
If no element is found for the given id, then None is returned.
Examples to get HTML element by ID using BeautifulSoup
1. Get HTML element whose id is "my_div"
In the following program, we take a sample HTML content in html_content variable, and then find the HTML element with id="my_div", using the above mentioned steps with BeautifulSoup.
Python Program
from bs4 import BeautifulSoup
# Sample HTML content
html_content = """
<html>
<body>
<div>This is div 1.</div>
<div>This is div 2.</div>
<div id="my_div">This is div 3.</div>
<div>This is div 4.</div>
</body>
</html>
"""
# Parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Get an element by its ID
element_with_id = soup.find(id="my_div")
# Check if the element was found
if element_with_id is not None:
print(element_with_id)
else:
print("Element not found.")
Output
<div id="my_div">This is div 3.</div>
We have found the element with the id="my_div", and printed the element to the standard output.
Summary
In this Python BeautifulSoup tutorial, given the HTML content string, we have seen how to find the HTML element with specific id attribute value, using BeautifulSoup.find() method.