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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With