日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python html压缩包,用python制作一个简单html压缩

發(fā)布時間:2025/3/15 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python html压缩包,用python制作一个简单html压缩 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡介

舉洪荒之力,集天地精華,親自操刀,用python玩一玩。

真是,雖有嘉肴,弗食,不知其旨也;雖有至道,弗學,不知其善也。

這個壓縮很low,并沒有什么留掰的,非常簡單,可以說只比壓縮前小一點點而已,

此乃缺點,優(yōu)點是不會出錯!!!

流程

思路

其實就是去回車而已,如果可以就加上去空格,不過去空格要定為兩個才能去,不然標簽會出錯。因為很low,所以js和css不壓縮。

程序載入→遍歷目錄文件→逐個壓縮

實現(xiàn)

個人比較懶,直接飲用網(wǎng)上的某函數(shù)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結(jié)尾的文件

print(file)

去注釋,改文件名,在用replace替換使路徑可用,加上判斷是否要這個路徑,修改后如下

修改函數(shù)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)

定義處理函數(shù)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 + " 壓縮完成!一共節(jié)省了" + 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 + " 壓縮完成!一共節(jié)省了" + str(long-short) + "個字符!")

total += long-short

print("本次壓縮共節(jié)省了%s個字符" %total)

input("回車退出!")

BUG

經(jīng)測試,用這個壓縮很容易把代碼框的縮進一起去掉。而縮進是python的靈魂……

解決方法: 再加個代碼框識別或放棄空格壓縮。放棄空格壓縮可直接在delete函數(shù)里的.replace(“ “,””)去掉。

總結(jié)

以上是生活随笔為你收集整理的python html压缩包,用python制作一个简单html压缩的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。