averageman averageman -4 years ago 92
HTML Question

Shortening HTML files

Is there a library (preferably a Python one) that shortens an HTML page? By that I mean that it will produce a possibly smaller (in terms of number of characters, including line breaks <- think about the length of a string) HTML page that is rendered exactly the same as the original one?

For instance:

Silly example

could be changed to:

<b>Silly example</b>

and the final result would be the same:

Silly example

Answer Source

You can use BeautifulSoup to prettify (not minify) HTML or XML code in Python.

from bs4 import BeautifulSoup
soup = BeautifulSoup('file.html')
prettified = soup.prettify(encoding="utf8")

For minifying HTML in Python you can use htmlmin. More parameters for htmlmin.minify can be found in the documentation.

import htmlmin

with open('file.html', 'r') as f:
    content = f.read()
    minified = htmlmin.minify(content, remove_empty_space=True)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download