import os
import time
import datetime
from urllib.parse import urlparse
from bs4 import BeautifulSoup
import glob
import qiniu
from qiniu import Auth, put_file, etag
# 配置七牛云的 Access Key 和 Secret Key
access_key = '-ocOUpLsZWSZ1dUc10XSnMVpVuCXuRSOz-pIb5pf'
secret_key = '2Cj6UQ_pTa2Nen_CLTlczideQQJ2hAhDqvOIvOQb'
bucket_name = 'jiass5'
qiniu_domain = 'cloud.jiass.cc' # 七牛云存储空间绑定的域名
# 初始化七牛云认证对象
q = Auth(access_key, secret_key)
def upload_to_qiniu(local_file):
# 创建以当前日期命名的文件夹
today = datetime.datetime.now().strftime("%Y%m")
key = f"{today}/{os.path.basename(local_file)}"
# 生成上传 Token
token = q.upload_token(bucket_name, key, 3600)
ret, info = put_file(token, key, local_file)
if info.status_code == 200:
return f'http://{qiniu_domain}/{key}'
else:
print(f'Upload failed: {local_file}')
return None
def process_html(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
soup = BeautifulSoup(f, 'html.parser')
# 查找所有img标签
img_tags = soup.find_all('img')
for img_tag in img_tags:
if 'src' in img_tag.attrs:
src = img_tag['src']
parsed_url = urlparse(src)
if not parsed_url.netloc:
# This is a local file
local_image_path = os.path.abspath(src)
if os.path.exists(local_image_path):
qiniu_url = upload_to_qiniu(local_image_path)
if qiniu_url:
img_tag['src'] = qiniu_url
else:
print(f'Failed to replace the image URL: {src}')
else:
print(f'Image not found: {src}')
else:
# This is a remote file, keep the original URL
pass
else:
print(f"Found an img tag without 'src' attribute: {img_tag}")
# 将修改后的HTML写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(str(soup))
# 读取当前文件夹中的HTML文件
html_files = glob.glob('./*.html')
total_files = len(html_files)
for index, file_path in enumerate(html_files):
print(f"Processing file {index + 1}/{total_files}: {file_path}") # 显示当前处理的文件及进度
process_html(file_path)
time.sleep(1) # 在每次迭代之间添加1秒延迟
文章来源:驾速速