Minimize HTML using Python with CLI
In a previous post, I showed you how-to minimize your HTML using Python. Well, the script will help you minimize your HTML but how about if you didn't want to edit the script each time you wanted to minimize a HTML file? This is where this tutorial comes in.
In a previous post, I showed you how-to minimize your HTML using Python. Well, the script will help you minimize your HTML but how about if you didn't want to edit the script each time you wanted to minimize a HTML file? This is where this tutorial comes in. In this tutorial, I'll show you how-to minimize your HTML file using Python but with command line interface (CLI).
import argparse
from bs4 import BeautifulSoup
# parse command line arguments
parser = argparse.ArgumentParser(description='Minimize HTML code')
parser.add_argument('input_file', type=str, help='path to the input HTML file')
parser.add_argument('output_file', type=str, help='path to the output HTML file')
args = parser.parse_args()
# read the HTML file
with open(args.input_file, 'r') as f:
html = f.read()
# parse the HTML using BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# remove whitespace
for element in soup.recursiveChildGenerator():
if hasattr(element, 'strip') and callable(element.strip):
element.strip()
# minimize the HTML
minimized_html = str(soup).replace('\n', '').replace('\t', '')
# write the minimized HTML to a file
with open(args.output_file, 'w') as f:
f.write(minimized_html)
Run the script using the following command:
python minimize_html.py src.html dist.html
Where src.html
is the input file and dist.html
is the output file.