Skip to content
Related Articles

Related Articles

Pretty-Printing in BeautifulSoup
  • Last Updated : 25 Feb, 2021

Prerequisite:

In this article, we will learn about how to print pretty in BeautifulSoup Using Python. The requests library is an integral part of Python for making HTTP requests to a specified URL. Whether it be REST APIs or Web Scrapping, requests are must be learned for proceeding further with these technologies. When one makes a request to a URI, it returns a response. Python requests provide inbuilt functionalities for managing both the request and response.

pip install requests

Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping.

pip install beautifulsoup4

What is Pretty Printing?

In simple words we can say, It prettifies the HTML with proper indents and everything.

Let’s Understand Step by step implementation:-



  • Import Required Module

Python3




# Import Required Module
import requests
from bs4 import BeautifulSoup
  • Parse HTML Content

Python3




# Web URL
Web_url = "Enter WEB URL"
  
# Get URL Content
r = requests.get(Web_url)
  
# Parse HTML Code
soup = BeautifulSoup(r.content, 'html.parser')
  • Pretty the HTML Code. BeautifulSoup has a prettify() method.

The prettify() method will turn a Beautiful Soup parse tree into a nicely formatted Unicode string, with a separate line for each tag and each string:

Python3




print(soup.prettify())

Below is the implementation:

Python3




# Import Required Module
import requests
from bs4 import BeautifulSoup
  
# Web URL
  
# Get URL Content
r = requests.get(Web_url)
  
# Parse HTML Code
soup = BeautifulSoup(r.content, 'html.parser')
print(soup.prettify())

Output:

python bs4 pretty printing

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :