illustrate
Recently, in the process of writing markdown documents, it is often necessary to insert some pictures. Because the server space for hosting the blog is limited, upload pictures to the picture bed and then insert them into markdown. Sometimes pictures from the Internet are inserted, and these pictures may become invalid at any time. My blog website shows an image error.
So it took a while to use python to match the image link in markdown, and then download the image and save it to a local folder img
. In this way, I don't have to worry about the image being invalid. When I find a stable image bed, I can Feel free to upload and update these pictures at any time, it's beautiful~~~
Function
.md
Automatic file search- Regular matching image link
- Crawl image content
- save text to local
step
Read file => Regular matching => Image download => Save locally
python code
For details, see my github address: https://github.com/wangshub/markdown-img-backup
# coding=utf-8
import sys
import os
import re
import requests
import urllib
import urllib2
def search(path, word):
for filename in os.listdir(path):
fp = os.path.join(path, filename)
if os.path.isfile(fp) and word in filename:
print fp
download(str(fp))
elif os.path.isdir(fp):
search(fp, word)
def download(file_path):
# filename = "test"
name = file_path.split(u"/")
filename = name[-1]
f_md = open(file_path)
# all text of md file
text = f_md.read().decode('utf-8')
# regex
img_reg = r'\!{1}\[(.*?)\]\((.*?)\)'
result = re.findall('!\[(.*)\]\((.*)\)', text)
for i in range(len(result)):
img_quote = result[i][0]
img_url = result[i][1]
# download img
request = urllib2.Request(img_url)
response = urllib2.urlopen(request)
img_contents = response.read()
# img name spell
urlname = img_url.split(u"/")
img_name = filename + '_' + \
str(i) + '_' + img_quote + str(urlname[len(urlname) - 1])
print img_name, '~~~', img_url
# write to file
f_img = open('img/' + img_name, 'wb')
f_img.write(img_contents)
f_img.close()
f_md.close()
search(sys.argv[1], '.md')
How to eat
python md_image_bacup.py /path/to/your/file/
author
Author : WangSong
E-mail : [email protected]