Beautiful Soup | prettify method
Start your free 7-days trial now!
prettify() method formats the Beautiful Soup parse tree so that there each tag is on its own separate line with indentation. It is used to easily visualize the structure of the Beautiful Soup parse tree.
Consider the following very hard to read HTML:
html = """<div>Bob<p>Sally<i>James</i></p></div>"""soup = BeautifulSoup(html)
prettify() method we can better understand its structure:
prettify() method adds whitespace to perform indentation, you should not use it for reformatting HTML and only use it for visualization (i.e. the meaning of HTML is changed due to the addition of whitespaces).