chatgpt生成的下载rpm镜像仓库Python脚本
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
# 目标URL
url = "https://update.cs2c.com.cn/NS/V10/V10SP3/os/adv/lic/base/aarch64/Packages/"
# 本地保存路径
save_dir = "D:/Packages/"
# 确保保存目录存在
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 获取网页内容
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
# 解析HTML
soup = BeautifulSoup(response.content, "html.parser")
# 找到所有链接
links = soup.find_all("a")
# 过滤出软件包链接
package_links = [link for link in links if link.get("href").endswith(".rpm")]
# 下载每个软件包
for link in package_links:
file_url = urljoin(url, link.get("href"))
file_name = os.path.join(save_dir, link.get("href"))
# 检查文件是否已存在
if os.path.exists(file_name):
print(f"File {file_name} already exists, skipping...")
continue
# 下载文件
print(f"Downloading {file_url} to {file_name}...")
response = requests.get(file_url, stream=True)
response.raise_for_status()
with open(file_name, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded {file_name}")
print("All downloads completed.")
本文来自博客园,作者:18cm的烦恼,转载请注明原文链接:https://www.cnblogs.com/jubian520/p/18204946