import requests
import re
from datetime import datetime, timedelta# 目标网页 URL
url = 'https://www.rarlab.com/'# 发送 HTTP GET 请求获取网页内容
response = requests.get(url)
response.raise_for_status() # 确保请求成功# 使用正则表达式查找连接文字为“Chinese Simplified”的下载链接
pattern = r'href="(/rar/winrar-x64-[^"]+sc\.exe)"[^>]*>Chinese Simplified<'
match = re.search(pattern, response.text, re.IGNORECASE)if match:# 补全下载链接download_link = f"https://www.rarlab.com{match.group(1)}"print(f"找到下载链接: {download_link}")# 发送 HTTP HEAD 请求获取文件的服务器时间head_response = requests.head(download_link)server_date = head_response.headers.get('Last-Modified')if server_date:# 将服务器时间转换为 datetime 对象server_date = datetime.strptime(server_date, '%a, %d %b %Y %H:%M:%S %Z')print(f"文件在服务器的时间: {server_date}")# 日期往前推一个月target_date = server_date - timedelta(days=30)file_name = match.group(1).split('/')[-1]for i in range(60): # 最多加60天# 生成新的日期格式new_date_str = target_date.strftime('%Y%m%d')new_url_x64 = f"https://www.win-rar.com/fileadmin/winrar-versions/sc/sc{new_date_str}/wrr/{file_name}"new_url_x32 = new_url_x64.replace('x64', 'x32')print(f"尝试访问: {new_url_x64}")# 发送 HTTP HEAD 请求测试 x64 文件是否存在head_response_x64 = requests.head(new_url_x64)if head_response_x64.status_code == 200:print(f"x64 文件存在: {new_url_x64}")# 下载 x64 文件download_response_x64 = requests.get(new_url_x64)download_response_x64.raise_for_status() # 确保下载成功# 保存 x64 文件到当前目录with open(file_name, 'wb') as file:file.write(download_response_x64.content)print(f"x64 文件已下载到当前目录: {file_name}")# 发送 HTTP HEAD 请求测试 x32 文件是否存在head_response_x32 = requests.head(new_url_x32)if head_response_x32.status_code == 200:print(f"x32 文件存在: {new_url_x32}")# 下载 x32 文件download_response_x32 = requests.get(new_url_x32)download_response_x32.raise_for_status() # 确保下载成功# 保存 x32 文件到当前目录file_name_x32 = file_name.replace('x64', 'x32')with open(file_name_x32, 'wb') as file:file.write(download_response_x32.content)print(f"x32 文件已下载到当前目录: {file_name_x32}")else:print(f"x32 文件不存在: {new_url_x32}")breakelse:print(f"文件不存在: {new_url_x64}")target_date += timedelta(days=1)else:print("无法获取文件的服务器时间")
else:print("未找到连接文字为'Chinese Simplified'的下载链接")