Open In App

BeautifulSoup – Remove the contents of tag

Last Updated : 25 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to remove the content tag from HTML using BeautifulSoup. BeautifulSoup is a python library used for extracting html and xml files.

Modules needed:

BeautifulSoup: Our primary module contains a method to access a webpage over HTTP.

For installation run this command into your terminal:

pip install bs4

Approach:

  • First, we will import the required libraries.
  • We will read the html file or text.
  • We will feed the extracted text to the soup object.
  • We will then find the required tag and then clear its element.

Step-by-step implementation:

Step 1: We will initialize the program, import the libraries and read or create the HTML doc that we want soup.

Python3




# Importing libraries
from bs4 import BeautifulSoup
  
# Reading the html text we want to parse
text = "<html> <head><title> Welcome </title></head><body><h1>This is a test page</h1></body></html>"


Step 2: We will pass the retrieved text to the soup object and set the parser in this case we are using html parser. Other markups that can be used are xml or html5. Then we will mention the tag from which we have to remove the content.

Python3




# creating a soup
soup = BeautifulSoup(text,"html.parser")
  
# printing the content in h1 tag
print(f"Content of h1 tag is: {soup.h1}")


Output:

Step 3: We will use .clear function. It clears the content of the mentioned tag.

Python3




# clearing the content of the tag
soup.h1.clear()
  
# printing the content in h1 tag after clearing
print(f"Content of h1 tag after clearing: {soup.h1}")


Below is the full implementation:

Python3




# Importing libraries
from bs4 import BeautifulSoup
  
# Reading the html text we want to parse
text = "<html> <head><title> Welcome </title></head><body><h1>This is a test page</h1></body></html>"
  
# creating a soup
soup = BeautifulSoup(text,"html.parser")
  
# printing the content in h1 tag
print(f"Content of h1 tag is: {soup.h1}")
  
# clearing the content of the tag
soup.h1.clear()
  
# printing the content in h1 tag after clearing
print(f"Content of h1 tag after clearing: {soup.h1}")




Previous Article
Next Article

Similar Reads

Change the tag's contents and replace with the given string using BeautifulSoup
Prerequisites: Beautifulsoup Beautifulsoup is a Python library used for web scraping. This powerful python tool can also be used to modify html webpages. This article depicts how beautifulsoup can be employed to change contents within a tag and replace the contents to be changed with the given string. For this, replace_with() function of the module
1 min read
BeautifulSoup - Append to the contents of tag
Prerequisites: Beautifulsoup Beautifulsoup is a Python library used to extract the contents from the webpages. It is used in extracting the contents from HTML and XML structures. To use this library, we need to install it first. Here we are going to append the text to the existing contents of tag. We will do this with the help of the BeautifulSoup
2 min read
How to extract a div tag and its contents by id with BeautifulSoup?
Beautifulsoup is a Python library used for web scraping. This powerful python tool can also be used to modify HTML webpages. This article depicts how beautifulsoup can be employed to extract a div and its content by its ID. For this, find() function of the module is used to find the div by its ID. Approach: Import moduleScrap data from a webpagePar
2 min read
Contents list - Python Beautifulsoup
The contents list is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The content is a list that contains the tag’s children.Syntax: tag.contents Below given examples explain the concept of contents in Beautiful
1 min read
BeautifulSoup object - Python Beautifulsoup
BeautifulSoup object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object. Syntax: BeautifulS
2 min read
Tag Object - Python Beautifulsoup
Tag object is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. Tag object corresponds to an XML or HTML tag in the original document. Further, this object is usually used to extract a tag from the whole HTML docu
2 min read
Get tag name using Beautifulsoup in Python
Prerequisite: Beautifulsoup Installation Name property is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. Name object corresponds to the name of an XML or HTML tag in the original document. Syntax: tag.name Para
1 min read
How to scrape all the text from body tag using Beautifulsoup in Python?
strings generator is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. One drawback of the string attribute is that it only works for tags with string inside it and returns nothing for tags with further tags insid
2 min read
Count the number of paragraph tag using BeautifulSoup
Sometimes, while extracting data from an HTML webpage, do you want to know how many paragraph tags are used in a given HTML document? Don't worry we will discuss about this in this article. Syntax:print(len(soup.find_all("p")))Approach: Step 1: First, import the libraries, BeautifulSoup, and os. from bs4 import BeautifulSoup as bs import os Step 2:
2 min read
How to insert a new tag into a BeautifulSoup object?
In this article, we will see how to insert a new tag into a BeautifulSoup object. See the below examples to get a better idea about the topic. Example: HTML_DOC : """ &lt;html&gt; &lt;head&gt; &lt;title&gt; Table Data &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; This is sample div 1 &lt;/div&gt; &lt;div&gt; This is sample div 2 &lt;/div
4 min read
Article Tags :
Practice Tags :