Last modified: Sep 06, 2023 By Alexander Williams
How to Use BeautifulSoup clear() Method
BeautifulSoup clear()
method is used to clean content from HTML elements. In this guide, we'll learn how to use this method with examples.
clear() syntax
To utilize the clear() method, follow this syntax:
element.clear()
element
: This represents the BeautifulSoup element you want to clear the contents of.
How to use clear() method
This basic example shows how to use clear() method:
Basic Example:
from bs4 import BeautifulSoup
html_content = """
<div>
<p>This is some text.</p>
<a href="https://example.com">Link</a>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Select an element and clear its contents
element_to_clear = soup.find('ul')
element_to_clear.clear()
# Print the modified HTML
print(soup.prettify())
Output:
<div>
<p>This is some text.</p>
<a href="https://example.com">Link</a>
<ul></ul>
</div>
As you can see in this example, we:
- Find
<ul>
element usingÂfind()
- Clear the element using
clear()
The result will show an empty <ul>
element.
Clean Content from multi-elements
If you want to clear the content from <li>
tags, follow this code:
from bs4 import BeautifulSoup
html_content = """
<div>
<p>This is some text.</p>
<a href="https://example.com">Link</a>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
"""
soup = BeautifulSoup(html_content, 'html.parser')
# Find all <li> elements
li_elements = soup.find_all('li')
# Clear the contents of each <li> element
for li in li_elements:
li.clear()
# Print the modified HTML
print(soup)
Output:
<div>
<p>This is some text.</p>
<a href="https://example.com">Link</a>
<ul>
<li></li>
<li></li>
</ul>
</div>
Here, we:
- Use the find_all() method to find all <li> tags.
- We iterate through each
<li>
element and use theclear()
method to remove the content within each<li>
element.
Conclusion
Today, we have learned how to use the clean()
method to remove content from one or more elements. This method can help you get more accurate results from your web scraping projects and simplify your parsing jobs.