Formatted printing
Beautiful Soup has two supported ways of printing. The first one is formatted printing that prints the current Beautiful Soup object into the formatted Unicode strings. Each tag is printed in a separate line with good indentation and this leads to the right look and feel. Beautiful Soup has the built-in method prettify()
for formatted printing. For example:
html_markup = """<p class="ecopyramid"> <ul id="producers"> <li class="producerlist"> <div class="name">plants</div> <div class="number">100000</div> </li> <li class="producerlist"> <div class="name">algae</div> <div class="number">100000</div> </li> </ul>""" soup = BeautifulSoup(html_markup,"lxml") print(soup.prettify())
The following screenshot shows the output:
In the output, we can see that <html><body>
gets appended. This is because Beautiful Soup uses the lxml
parser and it identifies any...