Minify your HTML

Online HTML Minifier/Compressor. Free! Provides an API. Simple Quick and Fast.

w3tees

Support HTML Minifier by purchasing a w3tees t-shirt. This site is also run by us.

Python

Credits: Thanks to Cristian Tomatis for contributing this example.

import requests

url = 'http://html-minifier.com/raw'
data = {'input': open('index.html', 'rb').read()}
response = requests.post(url, data=data)

print response.text

Python - from the REPL

Credits: Thanks to Noam Yorav-Raphael for contributing this example.

>>> import requests
>>> requests.post('http://html-minifier.com/raw', data=dict(input=' ... ')).text
'...'

Python - a CLI tool

Credits: Thanks to Kyle S. for contributing this to the site.

# -*- coding: utf-8 -*-
import sys
import requests

try:
    html_file = sys.argv[1]
except:
    print("Missing input file")
    sys.exit()

# Grab the file contents
with open(html_file, 'r') as c:
    html = c.read()

# Pack it, ship it    
payload = {'input': html}
url = 'http://html-minifier.com/raw'
print("Requesting mini-me of {}. . .".format(c.name))
r = requests.post(url, payload)

# Write out minified version
minified = html_file.rstrip('.html')+'.min.html'
with open(minified, 'w') as m:
    m.write(r.text)

print("Minification complete. See {}".format(m.name))

Language Examples

Click on the language of your choice:

wget Curl Node.js Python Ruby Perl PHP C#

More Languages

I'd love to add more examples in your favourite programming language, so feel free to send them my way. Email andychilton at that gmail place in the sky or tweet to me @andychilton.