jdk2588
jdk2588

Reputation: 792

Retain the children elements in beautifulSoup python?

I have markup as:

<p>Sample text. Click <a href="google.com">Here</a></p>

I want to replace the <p> tag with <span> without changing the children or text of the tag

Upvotes: 1

Views: 80

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23451

Your task can be done using replaceWith. You have to duplicate the element you want to use as the replacement, and then feed that as the argument to replaceWith. The documentation for replaceWith is pretty clear on how to do this.

In any case you could read this question How to change tag name with BeautifulSoup?

Upvotes: 1

Related Questions