w3resource

Python BeautifulSoup: Parse tree into a nicely formatted Unicode string, with a separate line for each HTML/XML tag and string

BeautifulSoup: Exercise-20 with Solution

Write a Python program to create a Beautiful Soup parse tree into a nicely formatted Unicode string, with a separate line for each HTML/XML tag and string.

Sample Solution:

Python Code:

from bs4 import BeautifulSoup
str1 = "<p>Some<b>bad<i>HTML Code</i></b></p>"
print("Original string:")
print(str1)
soup = BeautifulSoup("<p>Some<b>bad<i>HTML Code</i></b></p>", "xml")
print("\nFormatted Unicode string:")
print(soup.prettify())

Sample Output:

Original string:
<p>Some<b>bad<i>HTML Code</i></b></p>

Formatted Unicode string:
<?xml version="1.0" encoding="utf-8"?>
<p>
 Some
 <b>
  bad
  <i>
   HTML Code
  </i>
 </b>
</p>

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print the element(s) that has a specified id of a given web page.
Next: Write a Python program to find the first tag with a given attribute value in an html document.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Combining two iterable of tuples or pivot nested iterables:

# Combining two iterables
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> z = zip(a, b)
>>> z
[(1, 'a'), (2, 'b'), (3, 'c')]

# Pivoting list of tuples
>>> zip(*z)
[(1, 2, 3), ('a', 'b', 'c')]