Skip to content
Advertisement

Beautifulsoup: Replace all with aria-level attributes with tags of the same level

I have a HTML source where <div> elements serve as headings. Using Beautifulsoup and the attribute aria-level I would like to replace all <div> elements with <h> tags of the same level. My code kind of works for my purpose but it seems inelegant and ideally, the attributes of the former <div> elements would be removed.

JavaScript

Output:

JavaScript

What it should look like:

JavaScript

Advertisement

Answer

You can use del.attrs to delete all attributes from tag:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement