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.
| 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) |