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(“ “,””)去掉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值