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

淘宝做动图网站网站域名查询官网

淘宝做动图网站,网站域名查询官网,公司网站注销流程,我想做个卷帘门网站怎么做业务场景: AI生成的html文件,通常会使用多个cdn资源、手动替换or下载太过麻烦、如下py程序为此而生,指定html目录自动下载并替换~ import os import requests from bs4 import BeautifulSoup from urllib.parse import urlparse import has…

业务场景:
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.dtcms.com/wzjs/467332.html

相关文章:

  • 湖州住房和城乡建设局网站淘宝搜索排名
  • 网站外包费用怎么做分录seo能从搜索引擎中获得更多的
  • 什么样的水平可以做网站seo网站关键词优化软件
  • 陕西省人民政府采购网广州市口碑seo推广
  • 营销型网站分类google关键词搜索技巧
  • 赤峰网站策划百度推广渠道商
  • 如何找做网站的公司我想做app推广怎么做
  • 武汉高端企业网站建设中国网络优化公司排名
  • 网站建设改版沈阳网页建站模板
  • 网站谷歌seo做哪些网站流量统计软件
  • 武汉商城网站开发公司百度投诉中心入口
  • php和什么语言做网站东莞谷歌推广
  • 湘潭什么网站做c1题目关键词优化排名用什么软件比较好
  • 广州网站建设公众号推广一个6元
  • wordpress 需要缓存上海seo优化培训机构
  • 折800 网站模板网站长尾关键词排名软件
  • 厦门手机网站制作长沙网红打卡地
  • 郴州高椅岭安卓优化神器
  • 崇信县人民政府网站成都seo优化公司排名
  • 个人网站免备案吗首页百度
  • 手机版免费申请微网站福州模板建站哪家好
  • 黑龙江省建设教育信息网站seo标题优化分析范文
  • 北京海淀区网站建设微信广告推广价格表
  • 网络工作室创业项目seo行业岗位
  • ui设计培训项目湖南seo优化按天付费
  • 博彩类网站开发源代码广州网站优化推广
  • 杭州企业网站建设百度seo推广软件
  • 张家港优化网站seosemester
  • 网站建设教育国家市场监管总局
  • 开源网站后台网店代运营骗局