查看相同栏目文章
利用python修改html内容
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys from bs4 import BeautifulSoup fromDir="G:\\WorkSpace\\gitlab\\flow-web\\flow\\web" fsvg = open("logsvg.txt", encoding="utf-8",mode="w") fhtml=open("loghtml.txt", encoding="utf-8",mode="w") def printSvg(fromDir,times): files = os.listdir(fromDir) for fileName in files: path = os.path.join(fromDir,fileName) if os.path.isfile(path) and fileName[-3:] == "svg": fsvg.write(path+"\n") elif os.path.isfile(path) and fileName[-4:] == "html": f = open(path, encoding="utf-8",mode="r") content=f.read() f.close() soup = BeautifulSoup(content,'html.parser') finded=0 finds=soup.select('img[src$=".svg"]') for imgTag in finds: print(imgTag["src"]) finded=1 fixed_text = str(imgTag["src"]).replace('svg', 'png') print(path) print(imgTag.sourceline) print(str(fixed_text)) if times<1: print("---------------before-------------------") print(content) content=content.replace(str(imgTag["src"]),fixed_text) if times<1: print("---------------after-------------------") print(content) times+=1 if finded==1: fhtml.write(path+"\n") f = open(path, encoding="utf-8",mode="w") f.write(content) print("over write") f.close() #with open(path, "w",encoding="utf-8") as file: #file.write(str(soup)) elif os.path.isdir(path): printSvg(path,1) printSvg(fromDir,0) fsvg.close() fhtml.close() print(".........end............")