Skip to content Skip to sidebar Skip to footer

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 br

Solution 1:

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

from bs4 importBeautifulSoupsoup= 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

withopen('file.html', 'r') as f:
    content = f.read()
    minified = htmlmin.minify(content, remove_empty_space=True)

Solution 2:

You could use htmlmin.

import htmlmin

input_html = '<b>\n\tSilly example\n</b>'

minified_html = htmlmin.minify(input_html)

print(input_html)

# <b>#   Silly example# </b>print(minified_html)

# <b> Silly example </b>

Post a Comment for "Shortening Html Files"