Python BeautifulSoup: Replace a given tag with whatever’s inside a given tag

BeautifulSoup: Exercise-36 with Solution

Write a Python program to replace a given tag with whatever’s inside a given tag.

Sample Solution:

Python Code:

from bs4 import BeautifulSoup
markup = '<a href="https://w3resource.com/">Python exercises.<i>w3resource.com</i></a>'
soup = BeautifulSoup(markup, "lxml")
a_tag = soup.a
print("Original markup:")
print("\nAfter unwrapping:")

Sample Output:

Original markup:
<a href="https://w3resource.com/">Python exercises.<i>w3resource.com</i></a>

After unwrapping:
<a href="https://w3resource.com/">Python exercises.w3resource.com</a>

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to wrap an element in the specified tag and create the new wrapper.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Combining two iterable of tuples or pivot nested iterables:

# Combining two iterables
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> z = zip(a, b)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c')]

# Pivoting list of tuples
>>> zip(*z)
[(1, 2, 3), ('a', 'b', 'c')]