- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove empty tags using BeautifulSoup in Python?
BeautifulSoup is a python library that pulls out the data from HTML and XML files. Using BeautifulSoup, we can also remove the empty tags present in HTML or XML documents and further convert the given data into human readable files.
First, we will install BeautifulSoup library in our local environment using the command: pip install beautifulsoup4
#Import the BeautifulSoup library from bs4 import BeautifulSoup #Get the html document html_object = """ <p>Python is an interpreted, high-level and general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.</p> """ #Let us create the soup for the given html document soup = BeautifulSoup(html_object, "lxml") #Iterate over each line of the document and extract the data for x in soup.find_all(): if len(x.get_text(strip=True)) == 0: x.extract() print(soup)
Running the above code will generate the output and convert the given HTML document into human readable code by removing empty tags in it.
<html><body><p>Python is an interpreted, high−level and general−purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation.</p> </body></html>
- How to use Boto3 to remove tags in specified AWS secrets
- How can I parse a website using Selenium and Beautifulsoup in python?
- Python | Remove empty tuples from a list
- How to remove empty strings from a list of strings in Python?
- How to use Boto3 to remove tags from AWS Glue Resources
- How to create an empty file using Python?
- How to remove empty string/lines from PowerShell?
- Implementing Web Scraping in Python with BeautifulSoup?
- How to remove the HTML tags from a given string in Java?
- How to remove an empty string from a list of empty strings in C#?
- How to remove a directory using Python?
- How to remove a file using Python?
- How to remove swap files using Python?