1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import os import os.path as osp import shutil as sh import re from tqdm.auto import tqdm import requests
def makedir(root): if not osp.exists(root): os.mkdir(root)
def save_img(root,img_url): path=root+img_url.split('/')[-1] try: if not osp.exists(path): s = requests.session() s.keep_alive = False r = s.get(img_url) with open(path,'wb') as f: f.write(r.content) f.close() else: print(path+"文件已存在!") return 0 except Exception as e: print(img_url+", 爬取失败!") return 1 print(img_url+"已下载") return 0
def findimg(line,ori_pre): try: img = re.findall(f"({ori_pre}.*?(\.jfif|\.svg|\.webp|\.gif|\.jpeg|\.jpg|\.png|\.PNG|\.JPEG|\.JPG))",line)[0][0] name = img.split('/')[-1] except Exception as e: print("已自动忽略:",line) img,name = None,None return img,name
def changeurl(ori_root,save_root,down_root,ori_pre,new_pre): assert ori_pre[-1]=='/' and new_pre[-1]=="/" if osp.isdir(ori_root): files = os.listdir(ori_root) else: file = osp.basename(ori_root) ori_root = ori_root.split(file)[0] files = [file] makedir(save_root) makedir(down_root) for file in tqdm(files): print("Starting... ",file) with open(ori_root+file,'r',encoding = 'utf-8') as f: content = f.readlines() with open(save_root+file,'w',encoding='utf-8') as f: for line in content: if ori_pre in line: img,name = findimg(line,ori_pre) if img!=None: change = new_pre + name print(line,"==>",line.replace(img,change)) code = save_img(down_root,img) if code == 0: line = line.replace(img,change) f.write(line)
def main(): ori_root = './hexo/source/_posts/' save_root = './markdown/' down_root = './download/' ori_pre = 'https://cdn.jsdelivr.net/gh/' new_pre = 'https://unpkg.com/justlovesmile-post@1.0.3/' changeurl(ori_root,save_root,down_root,ori_pre,new_pre) if __name__ == "__main__": main()
|