markdown下载所有图片并保存
import re
from datetime import datetime
import requests
# 读取txt文件内容
with open('d:\Profile\Downloads\Verilog基础语法(7)之generate块.md', 'r', encoding='utf-8') as f:
content = f.read()
# 使用正则表达式匹配被()覆盖的图片url
pattern = r'\&\]\((.*?)\)'
urls = re.findall(pattern, content)
print(urls)
# 下载图片到本地并替换文本中对应的url
for i, url in enumerate(urls):
response = requests.get(url)
now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f')
filename = f'E:\\temp\\image_{i}_{now}.jpg'
with open(filename, 'wb') as f:
f.write(response.content)
# 替换文本中对应的url为本地图片路径
content = content.replace(url, filename)
# 将替换后的文本写入新文件中
with open('d:\Profile\Downloads\Verilog基础语法(7)之generate块_img.md', 'w', encoding='utf-8') as f:
f.write(content)
标签:-%,markdown,url,保存,datetime,content,下载,图片
From: https://www.cnblogs.com/sw-xiao/p/18525852