• 11-08 周三 解决csdn外链转存失败问题


    简介

     在使用typora + picGO撰写自己的博客时,图像已经转换为了静态资源,但发表在CSDN上,如果直接复制粘贴,csdn会将其中的图片链接再次转存到自己的图像服务器中,这有很大的问题。那么如何解决这个问题呢?

    问题描述

     在我们写好的markdown文件中,会存在很多如下的内容:
    在这里插入图片描述
     而csdn在检测到相关内容时,会自动进行外链图片下载和存储,但非常容易失败。一个一个下载又非常耗时耗力。

    外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
    而使用标签,则不需要进行转存。

    解决方式

    解决方式一: 使用vscode编辑器

    核心思想: 使用正则匹配,通过提取Group来实现功能。

     使用vs code进行替换。将模式替换为
    使用替换功能,开启正则表达式功能:
    在查询框中输入:

    !\[.*\]\((.*?)\)
    
    • 1

    然后在替换框中输入如下内容:

    
    
    • 1

    然后全局替换,即可完整markdown的图片转换为资源的,而且不会再次触发外链图像转存了。

    解决方式二: 代码片段

    
    # -*- coding: utf-8 -*-
    # @Author: yq1ng
    # @Date:   2021-05-20 17:22:08
    # @Last Modified by:   yq1ng
    # @Last Modified time: 2021-05-20 18:19:47
    
    import re
    import argparse
    import os
    
    def parse_argument():
        parser = argparse.ArgumentParser(description="main")
        parser.add_argument('--old_file', type=str, default=-1, help='original md file')
    
        return parser.parse_args()
    
    if __name__ == '__main__':
        args = parse_argument()
        old_path = args.old_file
        if not os.path.isfile(old_path):
            raise ValueError("请输入有效的文件")
        
        print(f"old_file: {old_path}")
        md_path = os.path.dirname(old_path)
        file_name = os.path.basename(old_path)
        new_path = os.path.join(md_path, "csdn-"+file_name)
        old_file = open(old_path, 'r', encoding='utf-8')
        new_file = open(new_path, 'w', encoding='utf-8')
    
        old_line = old_file.readline()
        count = 0
    
        while old_line:
            if "![" in old_line:
                url = re.findall('https://.*png|https://.*jpeg|https://.*jpg|https://.*JPG', old_line)
                img = '+ url[0] + '"/>'
                new_line = re.sub('!\[.*\)', img, old_line)
                new_file.write(new_line)
                print(old_line + '   ===>   ' + new_line)
                count += 1
            else:
                new_file.write(old_line)
            old_line = old_file.readline()
    
        old_file.close()
        new_file.close()
    
        print('\n已成功替换' + str(count) + '处外链问题')
        ```
        
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    Netty 进阶学习(九)-- 粘包与半包
    Flask 学习-9. 开启调试模式(debug模式)的2种方法
    C语言基础知识学习 -- 操作符和关键字,#define,指针
    想考PMP,符合报名条件么?怎么报考?
    Java11安装
    Pytorch中loss.backward()和torch.autograd.grad的使用和区别(通俗易懂)
    图像畸变与去畸变
    【JavaEE初阶】多线程 _ 基础篇 _ 线程安全问题(下篇)
    3-1、python内置数据类型(字符串类型)
    【RocketMQ中生产者生产消息的高可用机制、消费者消费消息的高可用机制、消息的重试机制、死信队列于死信消息】
  • 原文地址:https://blog.csdn.net/lk142500/article/details/134297021