python一键下载与替换hexo博客里的图片地址
2024-02-09 17:13:58 #Python 要求是在hexo博客里的图片地址都是网络地址, 然后把图片下载到本地, 然后替换掉原来的网络地址
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
|
import mimetypes, os, pathlib, re, requests
if __name__ == '__main__': p = pathlib.Path("blog/source") image_p = p / "images" post_p = p / "_posts" if not post_p.exists(): print(f"{post_p.absolute()} noy found") exit(1) else: os.chmod(post_p, 0o755) image_p.mkdir(exist_ok=True) os.chmod(p, 0o755) post_p.glob("*.md") for file in post_p.glob("*.md"): print(file) f = open(file, "r", encoding='utf8', errors='ignore') i = 0 content = f.read() f.close() for text in re.findall(r"\!\[[^\s].*\]\([a-zA-z]+:\/\/[^\s]*\)", content): print(text) tag = re.findall(r"\!\[[^\s].*\](?=\([a-zA-z]+:\/\/[^\s]*\))", text)[0] urldata = re.findall(r"(?<=\!\[[^\s].*\])\([a-zA-z]+:\/\/[^\s]*\)", text)[0] u = urldata[1:len(urldata) - 1] name = file.name + "-" + str(i) response = requests.get(u) content_type = response.headers['content-type'] extension = mimetypes.guess_extension(content_type) img = response.content with open(image_p / (name + extension), 'wb') as f: f.write(img) f.close() new_u = "/images/" + name + extension content = content.replace(text, tag + "(" + new_u + ")") i += 1 f = open(file, "w", encoding="utf8") f.write(content) f.close()
|