解决语雀转markdown后图片无法显示,将语雀导出的Markdown中的图片转换到本地

最近语雀的活动白嫖会员是嫖舒服了,可是到处文章为markdown的时候,发现了问题,没有图片,图片还是在语雀里,然而没联网的话就看不到图片emmmm
没办法咯,只好自己写个脚本把图片存下来呗

import re
import requests
import os

new_md = []


def deal_yuque(new_md, old_path, file_path):
    i = 0
    print('处理中...')
    with open(old_path, 'r', encoding='utf-8', errors='ignore') as f:
        for line in f.readlines():
            line = re.sub(r'png#(.*)+', 'png)', line)
            img_url = str(re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
                                     line)).replace("[", "").replace("'", "").replace(")", "").replace("]", "")
            if ('](https://' in img_url) and ('.png' in img_url):
                i += 1
                img_path = f'{file_path}\\img{i}.png'
                download_img(img_url, img_path)
                line = line.replace(img_url, img_path)
            new_md.append(line)
    with open(new_file, 'w', encoding='utf-8', errors='ignore') as f:
        for new_md in new_md:
            f.write(str(new_md))


def download_img(img_url, img_path):
    r = requests.get(img_url, stream=True)
    if r.status_code == 200:
        open(img_path, 'wb').write(r.content)
    del r


def mkdir(file_path):
    file_path = file_path.strip()
    file_path = file_path.rstrip("\\")
    isExists = os.path.exists(file_path)
    if not isExists:
        os.makedirs(file_path)
        return file_path


if __name__ == '__main__':
    old_path = input('原文件路径:')
    file_path = input('图片存储路径:')
    file_name = old_path.split("\\")[-1]
    new_path = '_' + file_name
    new_file = old_path.replace(file_name, "") + new_path
    file_path = mkdir(file_path)
    deal_yuque(new_md, old_path, file_path)
    print('处理完成')
    print('文件保存在:' + new_file)
    print('图片保存在:' + file_path)

转自:Bnessy-热爱自己所热爱的网络安全如需转载请申明出处!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bnessy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值