缩短HTML文件 [英] Shortening HTML files
问题描述
是否有一个缩短HTML页面的库(最好是一个Python)?我的意思是,它会产生一个可能更小的字符(包括换行符 - - 考虑字符串的长度),它的HTML页面与原始页面完全一样?
例如:
< b>
傻傻的例子
< / b>
可以更改为:
< b>傻瓜示例< / b>
,最后的结果将是相同的:
愚蠢的例子
.python.org / pypi / beautifulsoup4 /rel =nofollow> BeautifulSoup 用于在Python中对HTML或XML代码进行美化(不是缩小)。
from bs4 import BeautifulSoup
的更多参数
soup = BeautifulSoup('file.html')
prettified = soup.prettify(encoding =utf8)
$ c为了在Python中缩小HTML,你可以使用 nofollow的> htmlmin 。可以在 htmlmin.minify
$ bimport htmlmin
with open('file.html ','r')为f:
content = f.read()
minified = htmlmin.minify(content,remove_empty_space = True)
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)
这篇关于缩短HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!