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.

r=requests.get("https://proxyway.com/")

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

soup=BeautifulSoup(r.content,"html.parser")

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.

text_remove_tag=soup.find("title").get_text()

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

print(text_remove_tag)

Submit a comment

Your email address will not be published. Required fields are marked *

Rate this post