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.
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.
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)