This site has been acquired by Toptal.com.
(Attention! API endpoint has changed)

HTML Minifier

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

Python

Credits: Thanks to Cristian Tomatis for contributing this example.

import requests

url = 'https://www.toptal.com/developers/html-minifier/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('https://www.toptal.com/developers/html-minifier/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 = 'https://www.toptal.com/developers/html-minifier/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))

Languages

Click on the language of your choice to see an example: