home ~ projects ~ socials

Pretty Print HTML In Python With BeautifulSoup

from bs4 import BeautifulSoup as bs

input = """<html><head><title>
Pretty Print Example</title>
</head><body><p>Alfa Bravo Charlie</p>
</body></html>"""

soup = bs(input)

pretty_html = soup.prettify()

print(pretty_html)
Output:
<html>
 <head>
  <title>
   Pretty Print Example
  </title>
 </head>
 <body>
  <p>
   Alfa Bravo Charlie
  </p>
 </body>
</html>
-- end of line --