当前位置: 首页 > ai >正文

html文件cdn一键下载并替换

业务场景:
AI生成的html文件,通常会使用多个cdn资源、手动替换or下载太过麻烦、如下py程序为此而生,指定html目录自动下载并替换~
在这里插入图片描述

import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import hashlibclass CDNDownloader:def __init__(self, html_dir, static_dir='static'):self.html_dir = os.path.abspath(html_dir)self.static_dir = os.path.join(self.html_dir, static_dir)os.makedirs(self.static_dir, exist_ok=True)def process_all_html_files(self):"""处理指定目录下的所有HTML文件"""for root, _, files in os.walk(self.html_dir):for file in files:if file.endswith(('.html', '.htm')):file_path = os.path.join(root, file)print(f"Processing {file_path}...")self.process_single_html(file_path)def process_single_html(self, html_path):"""处理单个HTML文件"""with open(html_path, 'r', encoding='utf-8') as f:content = f.read()soup = BeautifulSoup(content, 'html.parser')# 计算HTML文件相对于根目录的层级rel_path = os.path.relpath(os.path.dirname(html_path), self.html_dir)path_prefix = '../' * len(rel_path.split(os.sep)) if rel_path != '.' else ''# 处理CSS文件for link in soup.find_all('link', rel='stylesheet'):if 'href' in link.attrs:old_url = link['href']if self._is_cdn_url(old_url):new_url = self.download_resource(old_url, 'css')# 根据HTML文件的位置调整相对路径link['href'] = f"{path_prefix}{new_url}"# 处理JS文件for script in soup.find_all('script', src=True):old_url = script['src']if self._is_cdn_url(old_url):new_url = self.download_resource(old_url, 'js')# 根据HTML文件的位置调整相对路径script['src'] = f"{path_prefix}{new_url}"# 保存修改后的HTML文件with open(html_path, 'w', encoding='utf-8') as f:f.write(str(soup))def _is_cdn_url(self, url):"""判断是否为CDN URL"""return url.startswith(('http://', 'https://', '//'))def download_resource(self, url, resource_type):"""下载资源并返回本地路径"""if url.startswith('//'):url = 'https:' + urltry:response = requests.get(url, timeout=10)if response.status_code == 200:# 使用URL的最后部分作为文件名,如果没有扩展名则添加filename = os.path.basename(urlparse(url).path)if not filename:# 如果URL没有文件名,使用URL的MD5作为文件名filename = hashlib.md5(url.encode()).hexdigest()filename = f"{filename}.{resource_type}"# 确保文件有正确的扩展名if not filename.endswith(f'.{resource_type}'):filename = f"{filename}.{resource_type}"# 创建资源类型子目录resource_dir = os.path.join(self.static_dir, resource_type)os.makedirs(resource_dir, exist_ok=True)file_path = os.path.join(resource_dir, filename)with open(file_path, 'wb') as f:f.write(response.content)# 返回相对于static目录的路径return os.path.join('static', resource_type, filename).replace('\\', '/')except Exception as e:print(f"Error downloading {url}: {str(e)}")return urlreturn urldef process_directory(html_dir, static_dir='static'):"""直接处理指定目录的便捷函数"""downloader = CDNDownloader(html_dir, static_dir)downloader.process_all_html_files()def main():# 方式1:命令行参数try:import argparseparser = argparse.ArgumentParser(description='Download CDN resources from HTML files')parser.add_argument('html_dir', help='Directory containing HTML files')parser.add_argument('--static-dir', default='static', help='Directory to save downloaded resources')args = parser.parse_args()process_directory(args.html_dir, args.static_dir)except SystemExit:# 方式2:写死在代码中的路径html_directories = ["templates",  # 示例路径1]for directory in html_directories:print(f"\n处理目录: {directory}")process_directory(directory)print("Done! All CDN resources have been downloaded and HTML files updated.")if __name__ == '__main__':main() 
http://www.xdnf.cn/news/7037.html

相关文章:

  • 第五部分:阶段项目 4:构建 RESTful API 服务器
  • 创建指定版本的vite项目
  • 《Python星球日记》 第88天:ChatGPT 与 LangChain
  • ADB基本操作和命令
  • 【机器学习】工具入门:飞牛启动Dify Ollama Deepseek
  • 开始学习做游戏,就现在
  • 短剧小程序系统开发源码上架,短剧项目市场分析
  • 【上位机——WPF】布局控件
  • vue3 elementplus tabs切换实现
  • Node.js 实战六:日志系统设计 —— 不只是 console.log,而是可追溯的行为记录链
  • Mac 在恢复模式下出现 旋转地球图标 但进度非常缓慢
  • 【android bluetooth 协议分析 01】【HCI 层介绍 3】【NUMBER_OF_COMPLETED_PACKETS 事件介绍】
  • 利用html制作简历网页和求职信息网页
  • Ubuntu 环境中 MATLAB 安装常见问题及解决方案
  • 【大模型系列】logprobs(对数概率)参数
  • boost变换器simulink仿真,开环,电压环控制,电流环控制,电压电流双闭环控制策略,含bode图
  • 从0开始学linux韦东山教程第四章问题小结(1)
  • React Native 0.68 安装react-native-picker报错:找不到compile
  • 整理了 2009 - 2025 年的【199 管综真题 + 解析】PDF,全套共 34 份文件
  • pycharm连接github(详细步骤)
  • 2025年- H28-Lc136- 24.两两交换链表中的节点(链表)---java版
  • 在超频单片机时,需要注意哪些稳定性问题?
  • 英语学习5.18
  • 深度估计中为什么需要已知相机基线(known camera baseline)?
  • 小白到高手的人工智能学习笔记之初步了解pytorch
  • 【图像生成大模型】HunyuanVideo:大规模视频生成模型的系统性框架
  • react深入2 - react-redux
  • LangChain框架实战:从入门到开发大模型应用
  • jupyter启动出现OSError: [Errno 28] No space left on device
  • LeetCode算 法 实 战 - - - 双 指 针 与 移 除 元 素、快 慢 指 针 与 删 除 有 序 数 组 中 的 重 复 项