How to remove tag, but keep its contents using Beautifulsoup

A step-by-step guide on how to remove a tag, but keep its contents using Beautifulsoup.

Removing a tag, but keeping its contents using Beautifulsoup is a really simple task – for each Tag object, you can use get_text() method. 

Important: we will use a real-life example in this tutorial, so you will need requests and Beautifulsoup libraries installed.

Step 1. Let’s start by importing the Beautifulsoup library.

from bs4 import BeautifulSoup

Step 2. Then, import requests library.

import requests

Step 3. Get a source code of your target landing page. We will be using our homepage in this example.


Step 4. Convert HTML code into a Beautifulsoup object named soup.


Step 5. Now, find the tag which content you would like to receive. In this example, we will try to get the contents of a title tag.


Step 6. Let’s check if our code works by printing it out.


Submit a comment

Your email address will not be published.

Rate this post