Python中如何实现自动裁剪掉CSDN复制的小尾巴——Windows剪切板正则提取工具

一个可以对剪切板监听,设置正则表达式进行过滤的小工具。 使用场景: 通过设置正则 ^([\s\S]*)---------------------[\s\S]*转载请附上博文链接!$ 复制代码的时候可以裁剪掉 csdn 强制的小尾巴,免得打断思路。 当然,如果是复制文章,还是要遵守人家规定的。

Github: https://github.com/gaoice/python-tools 打包好的 exe: https://pan.baidu.com/s/1hs94Qdm4gS_j6pdaOGc_qA 提取码: jcmi,进去下载 ClipboardFormat 这个压缩包。

❤❤❤


Python中如何实现自动裁剪掉CSDN复制的小尾巴——Windows剪切板正则提取工具

5 回复

禁用 CSDN 域名的 js 就完事了


这个问题很常见,CSDN复制代码时带的小尾巴确实烦人。我来写个工具,用pyperclipre就能搞定。

import re
import pyperclip
import time

def clean_csdn_tail(text):
    """
    清理CSDN复制内容的小尾巴
    匹配常见的几种CSDN小尾巴格式
    """
    patterns = [
        # 匹配 "作者:xxx 链接:xxx 来源:CSDN"
        r'作者:[\s\S]*?来源:CSDN.*$',
        # 匹配 "——来自CSDN"
        r'——\s*来自CSDN.*$',
        # 匹配 "来源:https://blog.csdn.net/xxx"
        r'来源:https://blog\.csdn\.net/.*$',
        # 匹配 "著作权归作者所有。"
        r'著作权归作者所有。.*$',
        # 匹配常见的CSDN声明块
        r'\n*\s*---\s*\n*.*CSDN.*$'
    ]
    
    cleaned = text
    for pattern in patterns:
        cleaned = re.sub(pattern, '', cleaned, flags=re.MULTILINE | re.IGNORECASE)
    
    # 清理末尾多余的空行
    cleaned = cleaned.rstrip()
    
    return cleaned

def monitor_clipboard():
    """监控剪切板,自动清理CSDN内容"""
    print("CSDN小尾巴清理工具已启动...")
    print("按 Ctrl+C 停止程序")
    print("-" * 40)
    
    last_content = ""
    
    try:
        while True:
            try:
                # 获取剪切板内容
                current_content = pyperclip.paste()
                
                # 如果内容有变化且包含CSDN相关关键词
                if (current_content != last_content and 
                    any(keyword in current_content.lower() 
                        for keyword in ['csdn', '著作权', '来源:'])):
                    
                    print(f"检测到新内容,长度: {len(current_content)} 字符")
                    
                    # 清理内容
                    cleaned = clean_csdn_tail(current_content)
                    
                    if cleaned != current_content:
                        # 将清理后的内容放回剪切板
                        pyperclip.copy(cleaned)
                        print("✓ 已清理CSDN小尾巴")
                        print(f"清理前: {len(current_content)} 字符")
                        print(f"清理后: {len(cleaned)} 字符")
                        print("-" * 40)
                    
                    last_content = cleaned
                else:
                    last_content = current_content
                    
            except Exception as e:
                print(f"处理出错: {e}")
            
            time.sleep(0.5)  # 降低CPU占用
            
    except KeyboardInterrupt:
        print("\n程序已停止")

if __name__ == "__main__":
    # 安装依赖:pip install pyperclip
    monitor_clipboard()

这个工具会一直监控剪切板,检测到CSDN内容就自动清理。核心是clean_csdn_tail()函数里的正则表达式,匹配了几种常见的CSDN小尾巴格式。

如果你只想单次清理,可以用这个简化版:

import re
import pyperclip

def clean_once():
    text = pyperclip.paste()
    cleaned = re.sub(r'作者:[\s\S]*?来源:CSDN.*$', '', text, flags=re.MULTILINE)
    cleaned = re.sub(r'——\s*来自CSDN.*$', '', cleaned, flags=re.MULTILINE)
    pyperclip.copy(cleaned.strip())
    print("清理完成!")

clean_once()

先装个依赖:pip install pyperclip,然后运行就行。工具会在后台自动清理,复制代码时就没那些烦人的小尾巴了。

正则匹配覆盖了常见的CSDN格式,如果遇到新的小尾巴样式,在patterns列表里加一条正则就行。

是的。这个主要是实现剪切板正则过滤提取,拿 csdn 举了个使用场景的栗子。🤝

阅读模式,格式不丢

window.getSelection=()=>{}

回到顶部