Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a structure of HTML code

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

like image 221
Milano Avatar asked Feb 13 '23 13:02

Milano


2 Answers

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>'
like image 75
roippi Avatar answered Feb 16 '23 03:02

roippi


Simple python regular expressions can do what you want:

import re

html = '''<html>
<body>
<h1>Simple example</h1>
<p>This is a simple example of html page</p>
</body>
</html>'''

structure = ''.join(re.findall(r'(</?.+?>|/n+?)', html))

This methods preserves newline characters.

like image 36
dwitvliet Avatar answered Feb 16 '23 02:02

dwitvliet