How to remove 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 the 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)
Results:
Congratulations, you’ve removed a tag, but kept its contents using Beautifulsoup. Here’s the full script:
from bs4 import BeautifulSoup
import requests
r=requests.get("https://proxyway.com/")
soup=BeautifulSoup(r.content,"html.parser")
text_remove_tag=soup.find("title").get_text()
print(text_remove_tag)