保存csdn的网页到本地,从本地打开会自动跳转到csdn官网的解决方案

由 阿添ATim 发布

环境需求:python
测试环境:python 3.12.4
将以下代码保存为xxx.py,运行即可

import os
import re

def find_and_remove_error_code(directory_path):
    for root, dirs, files in os.walk(directory_path):
        for filename in files:
            if filename.endswith(".html"):
                file_path = os.path.join(root, filename)
                with open(file_path, "r", encoding="utf-8") as file:
                    content = file.read()
                    pattern = r'<div style="display:none;">(.*?)</div>'
                    hidden_texts = re.findall(pattern, content, re.DOTALL)
                    if hidden_texts:
                        updated_content = re.sub(pattern, "", content, flags=re.DOTALL)
                        with open(file_path, "w", encoding="utf-8") as updated_file:
                            updated_file.write(updated_content)
                        print(f"{filename} 已删除相关代码")
                    else:
                        print(f"{filename} 没找到")

find_and_remove_error_code(os.getcwd())
input("按下任意键退出...")

0条评论

评论已关闭