Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortening HTML files

Tags:

python

html

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:

<b>
    Silly example
</b>

could be changed to:

<b>Silly example</b>

and the final result would be the same:

Silly example

like image 669
averageman Avatar asked Mar 20 '26 05:03

averageman


1 Answers

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)
like image 126
Christian Berendt Avatar answered Mar 21 '26 18:03

Christian Berendt