How to write the output to HTML file with Python BeautifulSoup? Last Updated : 08 Apr, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to write the output to an HTML file with Python BeautifulSoup. BeautifulSoup is a python library majorly used for web scraping but in this article, we will discuss how to write the output to an HTML file. Modules needed and installation: pip install bs4 Approach: We will first import all the required libraries.Make a get request to the desired URL and extract its page content.Using the file data type of python write the output in a new file. Steps to be followed: Step 1: Import the required libraries. Python3 # Import libraries from bs4 import BeautifulSoup import requests Step 2: We will perform a get request to the Google search engine home page and extract its page content and make a soup object out of it by passing it to beautiful soup, and we will set the markup as html.parser. Note: if you are extracting a xml page set the markup as xml.parser Python3 # set the url to perform the get request URL = 'https://www.geeksforgeeks.org/how-to-scrape-all-pdf-files-in-a-website/' page = requests.get(URL) # load the page content text = page.content # make a soup object by using beautiful # soup and set the markup as html parser soup = BeautifulSoup(text, "html.parser") Step 3: We use the file data type of python and write the soup object in the output file. We will set the encoding to UTF-8. We will use .prettify() function on soup object that will make it easier to read. We will convert the soup object to a string before writing it. We will store the output file in the same directory with the name output.html Python3 # open the file in w mode # set encoding to UTF-8 with open("output.html", "w", encoding = 'utf-8') as file: # prettify the soup object and convert it into a string file.write(str(soup.prettify())) Below is the full implementation: Python3 # Import libraries from bs4 import BeautifulSoup import requests # set the url to perform the get request URL = 'https://www.geeksforgeeks.org/how-to-scrape-all-pdf-files-in-a-website/' page = requests.get(URL) # load the page content text = page.content # make a soup object by using # beautiful soup and set the markup as html parser soup = BeautifulSoup(text, "html.parser") # open the file in w mode # set encoding to UTF-8 with open("output.html", "w", encoding = 'utf-8') as file: # prettify the soup object and convert it into a string file.write(str(soup.prettify())) Output: Comment More infoAdvertise with us Next Article How to write the output to HTML file with Python BeautifulSoup? H hg070401 Follow Improve Article Tags : Python Python BeautifulSoup Practice Tags : python Similar Reads How to write to an HTML file in Python ? Python language has great uses today in almost every field, it can be used along with other technologies to make our lives easier. One such use of python is getting the data output in an HTML file. We can save any amount of our input data into an HTML file in python using the following examples in t 2 min read How to Use lxml with BeautifulSoup in Python In this article, we will explore how to use lxml with BeautifulSoup in Python. lxml is a high-performance XML and HTML parsing library for Python, known for its speed and comprehensive feature set. It supports XPath, XSLT, validation, and efficient handling of large documents, making it a preferred 3 min read How to Scrape Websites with Beautifulsoup and Python ? Have you ever wondered how much data is created on the internet every day, and what if you want to work with those data? Unfortunately, this data is not properly organized like some CSV or JSON file but fortunately, we can use web scraping to scrape the data from the internet and can use it accordin 10 min read How to Import BeautifulSoup in Python Beautiful Soup is a Python library used for parsing HTML and XML documents. It provides a simple way to navigate, search, and modify the parse tree, making it valuable for web scraping tasks. In this article, we will explore how to import BeautifulSoup in Python. What is BeautifulSoup?BeautifulSoup 3 min read Converting HTML to Text with BeautifulSoup Many times while working with web automation we need to convert HTML code into Text. This can be done using the BeautifulSoup. This module provides get_text() function that takes HTML as input and returns text as output. Example 1: Python3 # importing the library from bs4 import BeautifulSoup # Init 1 min read Like