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.")

posted @ 2024-05-21 21:19  18cm的烦恼  阅读(2)  评论(0编辑  收藏  举报