Beautifulsoup: prettify() Example
Syntax
soup.prettify()
prettify() example
from bs4 import BeautifulSoup
# Html source
html = """
<div>
<h2>Recent Posts:</h2>
<li><a href="/post/beautifulsoup-how-to-use-head-examples/">Beautifulsoup: How to use .head Examples</a></li>
<li><a href="/post/how-to-solve-modulenotfounderror-no-module-named-whois/">How to solve ModuleNotFoundError: No module named 'whois'</a></li>
<li><a href="/post/python-how-to-use-whois-with-example/">Python: How to use whois() with example</a></li>
<li><a href="/post/beautifulsoup-get_text-example/">Beautifulsoup: get_text() Example</a></li>
<li><a href="/post/beautifulsoup-soupname-example/">BeautifulSoup: soup.name Example</a></li>
</div>
"""
# Parse
soup = BeautifulSoup(html, 'html.parser')
# Html beautifier
beautifier = soup.prettify()
# Print
print(beautifier)
Output:
<div>
<h2>
Recent Posts:
</h2>
<li>
<a href="/post/beautifulsoup-how-to-use-head-examples/">
Beautifulsoup: How to use .head Examples
</a>
</li>
<li>
<a href="/post/how-to-solve-modulenotfounderror-no-module-named-whois/">
How to solve ModuleNotFoundError: No module named 'whois'
</a>
</li>
<li>
<a href="/post/python-how-to-use-whois-with-example/">
Python: How to use whois() with example
</a>
</li>
<li>
<a href="/post/beautifulsoup-get_text-example/">
Beautifulsoup: get_text() Example
</a>
</li>
<li>
<a href="/post/beautifulsoup-soupname-example/">
BeautifulSoup: soup.name Example
</a>
</li>
</div>