.md
), 写法简单, 不用考虑排版, 输出的文章样式简洁优雅掘金
, 知乎(以文档方式导入)
, 简书(本来是最好用的, 最近在走下坡路)
)README.md
就是典型的markdown格式原来我喜欢在 掘金或简书后台 写markdown文章, 而后复制粘贴到 gitbook(前提是gitbook已经和github作了关联), 就能够发布到github仓库, 因为内容很吸引人, 在github收获一波stars(stars至关于点赞)python
但最近掘金和简书等平台忽然宣布, 在本身网站存储的图片再也不支持外链, 也就是在其它网站请求本站服务器存储的图片一概404 ! 简书是直接封了外链; 掘金发了一个公告, 延期一周执行;git
我只好将md文档保存到本地, 而后根据md保存的源图片信息,使用爬虫爬取图片到本地, 而后将图片上传到github仓库(github仓库支持图片上传, 并且不封外链), 将原图片信息替换为github仓库保存的图片信息github
/Users/lijianzhao/github
文件夹cd /Users/lijianzhao/github
git clone https://github.com/zhaoolee/GraphBed.git
复制代码
并保证 在此文件夹下, 有权限push到github, 权限添加方法 www.jianshu.com/p/716712278…bash
git clone https://github.com/zhaoolee/StarsAndClown.git
复制代码
md_images_upload.py
此脚本: ![]()
/Users/lijianzhao/github/GraphBed/images
目录;/Users/lijianzhao/github/GraphBed/images
目录下的全部图片, push到Githubimport os
import imghdr
import re
import requests
import shutil
import git
import hashlib
## 用户名
user_name = "zhaoolee";
## 仓库名
github_repository = "GraphBed";
## git仓库在本机的位置
git_repository_folder = "/Users/lijianzhao/github/GraphBed"
## 存放图片的git文件夹路径
git_images_folder = "/Users/lijianzhao/github/GraphBed/images"
## 设置忽略目录
ignore_dir_list=[".git"]
# 设置用户代理头
headers = {
# 设置用户代理头(为狼披上羊皮)
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
}
# 根据输入的url输入md5命名
def create_name(src_name):
src_name = src_name.encode("utf-8")
s = hashlib.md5()
s.update(src_name)
return s.hexdigest()
# 获取当前目录下全部md文件
def get_md_files(md_dir):
md_files = [];
for root, dirs, files in sorted(os.walk(md_dir)):
for file in files:
# 获取.md结尾的文件
if(file.endswith(".md")):
file_path = os.path.join(root, file)
print(file_path)
#忽略排除目录
need_append = 0
for ignore_dir in ignore_dir_list:
if(ignore_dir in file_path.split("/") == True):
need_append = 1
if(need_append == 0):
md_files.append(file_path)
return md_files
# 获取网络图片
def get_http_image(image_url):
image_info = {"image_url": "", "new_image_url": ""}
file_uuid_name = create_name(image_url)
image_data = requests.get(image_url, headers=headers).content
# 建立临时文件
tmp_new_image_path_and_name = os.path.join(git_images_folder, file_uuid_name)
with open(tmp_new_image_path_and_name, "wb+") as f:
f.write(image_data)
img_type = imghdr.what(tmp_new_image_path_and_name)
if(img_type == None):
img_type = ""
else:
img_type = "."+img_type
# 生成新的名字加后缀
new_image_path_and_name = tmp_new_image_path_and_name+img_type
# 重命名图片
os.rename(tmp_new_image_path_and_name, new_image_path_and_name)
new_image_url = "https://raw.githubusercontent.com/"+ user_name + "/" +github_repository+"/master/"+git_images_folder.split("/")[-1]+"/"+new_image_path_and_name.split("/")[-1]
image_info = {
"image_url": image_url,
"new_image_url": new_image_url
}
print(image_info)
return image_info
# 获取本地图片
def get_local_image(image_url):
image_info = {"image_url": "", "new_image_url": ""}
try:
# 建立文件名
file_uuid_name = uuid.uuid4().hex
# 获取图片类型
img_type = image_url.split(".")[-1]
# 新的图片名和文件后缀
image_name = file_uuid_name+"."+img_type
# 新的图片路径和名字
new_image_path_and_name = os.path.join(git_images_folder, image_name);
shutil.copy(image_url, new_image_path_and_name)
# 生成url
new_image_url = "https://raw.githubusercontent.com/"+ user_name + "/" +github_repository+"/master/"+git_images_folder.split("/")[-1]+"/"+new_image_path_and_name.split("/")[-1]
# 图片信息
image_info = {
"image_url": image_url,
"new_image_url": new_image_url
}
print(image_info)
return image_info
except Exception as e:
print(e)
return image_info
# 爬取单个md文件内的图片
def get_images_from_md_file(md_file):
md_content = ""
image_info_list = []
with open(md_file, "r+") as f:
md_content = f.read()
image_urls = re.findall(r"!\[.*?\]\((.*?)\)", md_content)
for image_url in image_urls:
# 处理本地图片
if(image_url.startswith("http") == False):
image_info = get_local_image(image_url)
image_info_list.append(image_info)
# 处理网络图片
else:
# 不爬取svg
if(image_url.startswith("https://img.shields.io") == False):
try:
image_info = get_http_image(image_url)
image_info_list.append(image_info)
except Exception as e:
print(image_url, "没法爬取, 跳过!")
pass
for image_info in image_info_list:
md_content = md_content.replace(image_info["image_url"], image_info["new_image_url"])
print("替换完成后::", md_content);
md_content = md_content
with open(md_file, "w+") as f:
f.write(md_content)
def git_push_to_origin():
# 经过git提交到github仓库
repo = git.Repo(git_repository_folder)
print("初始化成功", repo)
index = repo.index
index.add(["images/"])
print("add成功")
index.commit("新增图片1")
print("commit成功")
# 获取远程仓库
remote = repo.remote()
print("远程仓库", remote);
remote.push()
print("push成功")
def main():
if(os.path.exists(git_images_folder)):
pass
else:
os.mkdir(git_images_folder)
# 获取本目录下全部md文件
md_files = get_md_files("./")
# 将md文件依次爬取
for md_file in md_files:
# 爬取单个md文件内的图片
get_images_from_md_file(md_file)
git_push_to_origin()
if __name__ == "__main__":
main()
复制代码
几个优化点:
- 支持md引用本地目录图片的爬取(之后就能够在本地编写markdown文件了, 编写完成后, 运行上述脚本, 便可自动将md引用的本地图片上传到github, 同时本地图片的引用地址被github在线图片地址所取代)
- 为防止图片重名, 使用uuid重命名图片名称(后面发现使用uuid会致使相同的网络图片反复爬取保存, 因此后面使用网络图片的url地址对应的md5码为新名称, 便可防止生成内容相同, 名称不一样的图片)
- 爬取本地图片,依然使用uuid重名防止重复(我的命名可能会反复使用
001.png
,002.png
等经常使用名称)- 对爬取的图片, 进行了类型判断, 自动补充图片扩展名
安装方法见 Python数据挖掘 环境搭建服务器
md_images_upload.py
放到/Users/lijianzhao/github/GraphBed
目录 (这里目录能够按照本身的来, 但脚本顶部的几行参数也要修改)pip3 install requests
pip3 install git
复制代码
/Users/lijianzhao/github/GraphBed
cd /Users/lijianzhao/github/GraphBed
复制代码
python3 md_images_upload.py
复制代码
这里我已是第二次替换图片了, 因此上面的动图显示的原图片也是GitHub的图片, 说明脚本第一次已彻底替换成功~ ![]()
图片又能够显示了markdown
被替换为github图片替换后md在线展现地址: zhaoolee.gitbooks.io/starsandclo…网络