Get a structure of HTML code
beautifulsoup, html, python
Solution
There is not, to my knowledge, but a little recursion should work:
def taggify(soup):
for tag in soup:
if isinstance(tag, bs4.Tag):
yield '<{}>{}</{}>'.format(tag.name,''.join(taggify(tag)),tag.name)
demo:
html = '''<html>
<body>
<h1>Simple example</h1>
<p>This is a simple example of html page</p>
</body>
</html>'''
soup = BeautifulSoup(html)
''.join(taggify(soup))
Out[34]: '<html><body><h1></h1><p></p></body></html>'
Problem
I'm using BeautifulSoup4 and I'm curious whether is there a function which returns a structure (ordered tags) of the HTML code. Here is an example: ``` <html> <body> <h1>Simple example</h1> <p>This is a simple example of html page</p> </body> </html> ``` print page.structure(): ``` >> <html> <body> <h1></h1> <p></p> </body> </html> ``` I tried to find a solution but no success. Thanks