2011-04-08 747 views
5

如何从python压缩(最小化)HTML;我知道我可以使用一些正则表达式去除空格和其他东西,但是我想要一个真正的使用纯Python的编译器(所以它可以在Google App Engine上使用)。压缩(最小化)来自python的HTML

我做了一个在线html压缩器的测试,它节省了65%的html大小。我想要那个,但是来自python。

回答

6

您可以使用htmlmin来缩小你的HTML:

import htmlmin 

html = """ 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Bootstrap Case</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
<div class="container"> 
    <h2>Well</h2> 
    <div class="well">Basic Well</div> 
</div> 
</body> 
</html> 
""" 

minified = htmlmin.minify(html.decode("utf-8"), remove_empty_space=True) 
print(minified) 
4

我想在GAE没有真的需要缩小你的HTML作为GAE已经gzip压缩它Caching & GZip on GAE (Community Wiki)

我没有测试,但HTML的精缩版可能会获胜只有1%的大小,因为它只是删除空间一旦两个版本都被压缩。

如果你想节省空间,例如通过memcached的话,你必须比在蟒蛇消除空间更感兴趣gzip压缩它(即使在压缩的低电平)在C处理相反,它很可能是更小,更快纯Python

+0

感谢您指出我出这一点。我在日志中看到一些浏览器尚不支持gzip;但再次查看日志,我得到的要求并不多。 – 2011-04-08 15:42:24

+5

删除65%的原始HTML可能不会在压缩时保存65%,但它仍然可以节省一些东西。 – geoffspear 2011-04-09 14:10:31

+0

另外不要忘记,有时候html会存储在memcache上,你很难在之前压缩它 – 2011-05-18 12:21:48

0

的我写的复制我的模板到另一个目录中生成脚本,然后我用这种伎俩来告诉我的应用程序选择,或在生产发展模式正确的模板:

DEV = os.environ['SERVER_SOFTWARE'].startswith('Development') and not PRODUCTION_MODE 

TEMPLATE_DIR = 'templates/2012/head/' if DEV else 'templates/2012/output/' 

无论它是由你的网络服务器gzipped是不是真的重点,你应该保存每个字节,你可以为性能的原因。

如果你在一些最大的网站看看在那里,他们经常做这样的事情写无效的HTML保存字节,例如,常见的是省略双引号ID在html标签属性,例如:

<did id=mydiv> ... </div> 

相反的:

<did id="mydiv"> ... </div> 

而且有像这样的几个例子,但是这线程我猜的范围旁边。

回到问题,我放了一个小的生成脚本,缩小您的HTML,CSS和JS。警告:它不包括PRE标签的情况。

import os 
import re 
import sys 

from subprocess import call 

HEAD_DIR = 'templates/2012/head/' 

OUT_DIR = 'templates/2012/output/' 

REMOVE_WS = re.compile(r"\s{2,}").sub 

YUI_COMPRESSOR = 'java -jar tools/yuicompressor-2.4.7.jar ' 

CLOSURE_COMPILER = 'java -jar tools/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS ' 

def ensure_dir(f): 
    d = os.path.dirname(f) 
    if not os.path.exists(d): 
     os.makedirs(d) 

def getTarget(fn): 
    return fn.replace(HEAD_DIR, OUT_DIR) 

def processHtml(fn, tg): 
    f = open(fn, 'r') 
    content = f.read() 
    content = REMOVE_WS(" ", content) 
    ensure_dir(tg) 
    d = open(tg, 'w+') 
    d.write(content) 
    content 

def processCSS(fn, tg): 
    cmd = YUI_COMPRESSOR + fn + ' -o ' + tg 
    call(cmd, shell=True) 
    return 

def processJS(fn, tg): 
    cmd = CLOSURE_COMPILER + fn + ' --js_output_file ' + tg 
    call(cmd, shell=True) 
    return 

# Script starts here. 
ensure_dir(OUT_DIR) 
for root, dirs, files in os.walk(os.getcwd()): 
    for dir in dirs: 
    print "Processing", os.path.join(root, dir) 
    for file in files: 
    fn = os.path.join(root) + '/' + file 
    if fn.find(OUT_DIR) > 0: 
     continue 
    tg = getTarget(fn) 
    if file.endswith('.html'): 
     processHtml(fn, tg) 
    if file.endswith('.css'): 
     processCSS(fn, tg) 
    if file.endswith('.js'): 
     processJS(fn, tg)  
1

htmlminhtml_slimmer是Python的一些简单的HTML缩小文件的工具。我有数百万的html页面存储在我的数据库中,并运行htmlmin,我可以将页面大小缩小5%到50%。他们都没有在完成html缩小时做出最佳工作(即字体颜色#00000可以减少到#000),但这是一个好的开始。我有一个try/except块运行htmlmin,然后如果失败,html_slimmer,因为htmlmin似乎提供更好的压缩,但它不支持非ascii字符。

示例代码:

import htmlmin 
from slimmer import html_slimmer # or xhtml_slimmer, css_slimmer 
try: 
    html=htmlmin.minify(html, remove_comments=True, remove_empty_space=True) 
except: 
    html=html_slimmer(html.strip().replace('\n',' ').replace('\t',' ').replace('\r',' ') ) 

祝您好运!