需求

给定一个markdown文件,其中的图片链接均来自网络,如何用python写一下脚本以实现快速将其中所有的图片从其所在的网站中下载下来。

代码示例

download_md_pics.py

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
import requests
import re

def download_images_from_md(md_file):
with open(md_file, 'r', encoding='utf-8') as file:
content = file.read() # 读取markdown文件

# 使用正则表达式提取markdown图片链接:
img_urls = re.findall('!\[.*?\]\((.*?)\)', content)

# 下载其中的图片:
for url in img_urls:
try:
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功,跳过失效图片
if response.status_code == 200:
# 提取图片文件名
filename = url.split('/')[-1]
with open(filename, 'wb') as image_file:
image_file.write(response.content)
print(f"Downloaded: {filename}")
except (requests.exceptions.RequestException, IOError) as e:
print(f"Failed to download: {url}")
print(f"Error: {e}")

# 指定被提取的 Markdown 文件全路径
md_file_path = './test.md'

# 调用函数下载图片
download_images_from_md(md_file_path)

运行结果

1
python download_md_pics.py

所有test.md中指向的图片链接都被下载了下来: