home ~ socials ~ projects ~ rss

Pretty Print HTML In Python With BeautifulSoup

December 2023
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
Share link:
https://www.alanwsmith.com/en/2z/bb/x7/ed/?pretty-print-html-in-python-with-beautifulsoup