Python下载文件到指定文件夹

Python下载文件到指定文件夹

import os  # 导入os库
import urllib.request  # 导入urllib库
import requests as rb  # 导入requests库
from bs4 import BeautifulSoup  # 调用beautifulsoup库

#######文件下载
def file_downloand(fileUrl):
    # 文件基准路径
    basedir = "C:/Users/Administrator/Downloads/fileDownload"
    # 如果没有这个path则直接创建
    if not os.path.exists(basedir):
        os.makedirs(basedir)
    # 下载到服务器的地址
    file_path = basedir + '/' + fileUrl[fileUrl.rfind("/") + 1:len(fileUrl)]
    if os.path.exists(file_path) == False:  # 判断是否存在文件
        # 文件url
        try:
            print("下载链接:" + fileUrl)
            urllib.request.urlretrieve(fileUrl, filename=file_path)
            print("成功下载文件")
        except IOError as exception_first:  # 设置抛出异常
            print(1, exception_first)
        except Exception as exception_second:  # 设置抛出异常
            print(2, exception_second)
    else:
        print("文件已经存在!")


def get_download_url_from_page(base_url):
    fileList = []
    data = rb.get(base_url)  # 获取HTML网页,对应HTTP的GET
    soup = BeautifulSoup(data.text, "html.parser")  # 使用BeautifulSoup解析获取到的数据
    links = []  # 定义空列表links
    for link in soup.find_all("a"):
        # 输出网页中的a标签下的href内容到links中;; append()方法用于在列表末尾添加新的对象
        print(link.get("href"))
        if link.get("href").count('java') == 1:
            links.append(link.get("href"))
    for link_url in links:
        data_file = rb.get(base_url + link_url)
        soup_file = BeautifulSoup(data_file.text, "html.parser")
        for link_file in soup_file.find_all("a"):
            if str(link_file.get("href")).count('jar') == 1 and str(link_file.get("href")).count('jar.asc') != 1:
                fileList.append(base_url + link_url + link_file.get("href"))
    return fileList


fileList = get_download_url_from_page("https://mirrors.tuna.tsinghua.edu.cn/mariadb/")
# fileUrl = "https://unraid-dl.sfo2.cdn.digitaloceanspaces.com/stable/unRAIDServer-6.10.3-x86_64.zip"
for fileUrl in fileList:
    file_downloand(fileUrl)
# print(fileUrl[fileUrl.rfind("/") + 1:len(fileUrl)])

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值