python html压缩包,用python制作一个简单html压缩
簡介
舉洪荒之力,集天地精華,親自操刀,用python玩一玩。
真是,雖有嘉肴,弗食,不知其旨也;雖有至道,弗學,不知其善也。
這個壓縮很low,并沒有什么留掰的,非常簡單,可以說只比壓縮前小一點點而已,
此乃缺點,優點是不會出錯!!!
流程
思路
其實就是去回車而已,如果可以就加上去空格,不過去空格要定為兩個才能去,不然標簽會出錯。因為很low,所以js和css不壓縮。
程序載入→遍歷目錄文件→逐個壓縮
實現
個人比較懶,直接飲用網上的某函數import os
def getFiles(dir, suffix): # 查找根目錄,文件后綴
res = []
for root, directory, files in os.walk(dir): # =>當前根,根下目錄,目錄下的文件
for filename in files:
name, suf = os.path.splitext(filename) # =>文件名,文件后綴
if suf == suffix:
res.append(os.path.join(root, filename)) # =>吧一串字符串組合成路徑
return res
for file in getFiles("./", '.py'): # =>查找以.py結尾的文件
print(file)
去注釋,改文件名,在用replace替換使路徑可用,加上判斷是否要這個路徑,修改后如下
修改函數import os
def getFiles(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf == suffix:
res.append(os.path.join(root, filename))
return res
for file in getFiles(r"html所在文件夾路徑", '.html'):
if file == r"D:\blog\public\404.html":
continue
elif:
pass
path = file.replace("\\","\\\\")
print(path)
定義處理函數def delete(string):
res = string.replace("\n","").replace(" ","")
return res
流程text_list = []
with open(path,"r",encoding="UTF-8") as f:
Not_Change = False
for each in f.readlines():
long += len(each)
if "
text_list.append(each)
continue
if "
text_list.append(each)
continue
if "
text_list.append(each)
continue
if "" in each:
Not_Change = False
text_list.append(delete(each))
continue
if "
text_list.append(each)
continue
if "" in each:
Not_Change = False
text_list.append(delete(each))
continue
if Not_Change:
text_list.append(each)
else:
text_list.append(delete(each))
with open(path,"w",encoding="UTF-8") as f:
for each in text_list:
short += len(each)
f.write(each)
print(file + " 壓縮完成!一共節省了" + str(long-short) + "個字符!")
END
簡直是絞盡腦汁,哈哈哈!壓縮這玩意,還行!
最后附上完整代碼!import os
total = 0
def getFiles(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf == suffix:
res.append(os.path.join(root, filename))
return res
def delete(string):
res = string.replace("\n","").replace(" ","")
return res
for file in getFiles(r"D:\blog\public", '.html'):
if file == r"D:\blog\public\404.html":
continue
elif True:
pass
path = file.replace("\\","\\\\")
long = 0
short = 0
text_list = []
with open(path,"r",encoding="UTF-8") as f:
Not_Change = False
for each in f.readlines():
long += len(each)
if "
text_list.append(each)
continue
if "
text_list.append(each)
continue
if "
text_list.append(each)
continue
if "" in each:
Not_Change = False
text_list.append(delete(each))
continue
if "
text_list.append(each)
continue
if "" in each:
Not_Change = False
text_list.append(delete(each))
continue
if Not_Change:
text_list.append(each)
else:
text_list.append(delete(each))
with open(path,"w",encoding="UTF-8") as f:
for each in text_list:
short += len(each)
f.write(each)
print(file + " 壓縮完成!一共節省了" + str(long-short) + "個字符!")
total += long-short
print("本次壓縮共節省了%s個字符" %total)
input("回車退出!")
BUG
經測試,用這個壓縮很容易把代碼框的縮進一起去掉。而縮進是python的靈魂……
解決方法: 再加個代碼框識別或放棄空格壓縮。放棄空格壓縮可直接在delete函數里的.replace(“ “,””)去掉。
總結
以上是生活随笔為你收集整理的python html压缩包,用python制作一个简单html压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Opencv 学习笔记之——鼠标操作画
- 下一篇: C++学习笔记之——引用 内联函数