批量为css/js增加v=xxx
Posted abcyrf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量为css/js增加v=xxx相关的知识,希望对你有一定的参考价值。
原始参考代码:https://download.csdn.net/download/liuj1314/11351556
import os
import re
import sys
def file_extension(path):
return os.path.splitext(path)[1]
basePath = sys.path[0]
# if len(sys.argv)==1:
# sys.exit()
pass_file = [
]
html_list = []
def find_html(path):
files = os.listdir(path)
for item in files:
abs_path = os.path.join(path, item)
if not os.path.isdir(abs_path) and file_extension(abs_path) == ".html":
html_list.append(abs_path)
if (os.path.isdir(abs_path)):
find_html(abs_path)
def deal_html(html_list, ver):
for html_path in html_list:
html_file = open(html_path, "r+")
content = html_file.read()
# print(html_file.read())
# res = re.sub(r'<link (.*) href="(.*)\.css".*>',r'<link \1 href="\2\.css?v=1"\3>',content)
res1 = re.sub(r'<link(.*)href(.*)="(.*)\.css(.*?)"(.*)>', re_css, content)
res2 = re.sub(r'<script(.*)src(.*)="(.*)\.js(.*?)"(.*)>',
re_script, res1)
html_file.seek(0)
html_file.truncate()
html_file.write(res2)
# print(html_file.read())
html_file.close()
def IsPass(temp, ver):
x = temp.split('/')
val = x[len(x) - 1]
for s in pass_file:
if s.find(val) >= 0:
return ''
return '?v=' + ver
def re_css(x):
return '<link' + x.group(1) + 'href="' + x.group(3) + '.css' + IsPass(x.group(3), ver) + '"' + x.group(5) + '>'
def re_script(x):
return '<script' + x.group(1) + 'src="' + x.group(3) + '.js' + IsPass(x.group(3), ver) + '"' + x.group(
5) + '>'
if __name__ == '__main__':
# num=['0','1','2','3','4','5','6','7','8','9']
# ran=random.sample(num,5)
# ver=str.join(ran).replace(" ","")
ver = "0.0.1"
path = ""
find_html("D:\\code\\templates")
deal_html(html_list, ver)
以上是关于批量为css/js增加v=xxx的主要内容,如果未能解决你的问题,请参考以下文章