当前位置: 代码网 > it编程>前端脚本>Python > python如何下载网络文件到本地指定文件夹

python如何下载网络文件到本地指定文件夹

2025年05月18日 Python 我要评论
在python中下载文件到本地指定文件夹可以通过以下步骤实现,使用requests库处理http请求,并结合os模块处理文件路径:import requests, os,datetimefrom ur

 在python中下载文件到本地指定文件夹可以通过以下步骤实现,使用requests库处理http请求,并结合os模块处理文件路径:

import requests, os,datetime
from urllib.parse import urlparse,parse_qs
 
"""
获取request header信息,cookie根据网址需要自己设定
"""
headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7;application/json, text/javascript, */*; q=0.01',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-cn,zh;q=0.9',
    'cache-control': 'no-cache',
    'connection': 'keep-alive',
    'cookie': '132213213213213213',
    'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/66.0.3359.139 safari/537.36'
}
 
 
def download_file(url, save_dir=none,file_name=none):
    """
    下载文件并保存到指定路径,,从网络路径中获取文件名,例如https://127.0.0.1:8000/web/file/5ed63734774b40d181fd96e1c58133d2.pdf
    :param url: 文件下载url
    :param save_dir: 文件保存路径
    :param file_name: 文件名
    """
    try:
        if file_name is none:
            parse_url=urlparse(url)
            file_name=os.path.basename(parse_url.path)
            if not file_name:
                file_name="download_file_"+datetime.datetime.now().strftime("%y%m%d%h%m%s")
        if save_dir is none:
            save_dir=rf"c:\users\desktop\download_file"
        save_path=os.path.abspath(save_dir)
        file_path=os.path.join(save_path,file_name)
 
        if save_dir and not os.path.exists(save_dir):
            os.makedirs(save_dir, exist_ok=true)
 
        # 发起带流式传输的get请求
        with requests.get(url, stream=true,headers=headers) as response:
            response.raise_for_status()  # 检查http状态码
 
            # 分块写入文件
            with open(file_path, 'wb') as file:
                for chunk in response.iter_content(chunk_size=8192):
                    if chunk:  # 过滤保持连接的空白块
                        file.write(chunk)
 
        print(f"文件下载成功,保存路径:{file_path}")
        return true
    except requests.exceptions.requestexception as e:
        print(f"网络请求失败:{str(e)}")
    except ioerror as e:
        print(f"文件操作失败:{str(e)}")
    except exception as e:
        print(f"未知错误:{str(e)}")
 
    return false
 
 
def download_file2(url, save_dir=none,file_name=none):
    """
        下载文件并保存到指定路径,从网络路径中获取文件名,例如https://127.0.0.1:8000/web/file/5ed63734774b40d181fd96e1c58133d2.pdf
        :param url: 文件下载url
        :param save_dir: 文件保存路径
        :param file_name: 文件名
    """
    try:
        if file_name is none:
            parse_url = urlparse(url)
            file_name = os.path.basename(parse_url.path)
            if not file_name:
                file_name = "download_file_" + datetime.datetime.now().strftime("%y%m%d%h%m%s")
        if save_dir is none:
            save_dir = rf"c:\users\desktop\download_file"
        save_path = os.path.abspath(save_dir)
        file_path = os.path.join(save_path, file_name)
 
        if save_dir and not os.path.exists(save_dir):
            os.makedirs(save_dir, exist_ok=true)
 
        response = requests.get(url, stream=true, headers=headers)
        response.raise_for_status()
        with open(file_path, 'wb') as file:
            file.write(response.content)
        return true
    except requests.exceptions.requestexception as e:
        print(f"网络请求失败:{str(e)}")
    except ioerror as e:
        print(f"文件操作失败:{str(e)}")
    except exception as e:
        print(f"未知错误:{str(e)}")
 
    return false
 
 
def download_file3(url, save_dir=none,file_name=none):
    """
        下载文件并保存到指定路径,从网络路径中query参数中获取文件名,例如https://127.0.0.1:8000/web/file?path=2025041616372016\\5ed63734774b40d181fd96e1c58133d2.pdf
        :param url: 文件下载url
        :param save_dir: 文件保存路径
        :param file_name: 文件名
    """
    try:
        if file_name is none:
            parse_url = urlparse(url)
            # 从query参数中提取文件名
            query_params = parse_qs(parse_url.query)
            # 获取 path 参数的值,path可根据实际情况进行调整
            path_value = query_params.get('path', [''])[0]
 
            # 提取文件名(最后一个反斜杠后的部分)
            file_name = path_value.split('\\')[-1]
            if not file_name:
                file_name = "download_file_" + datetime.datetime.now().strftime("%y%m%d%h%m%s")
        if save_dir is none:
            save_dir = rf"c:\users\desktop\download_file"
        save_path = os.path.abspath(save_dir)
        file_path = os.path.join(save_path, file_name)
 
        if save_dir and not os.path.exists(save_dir):
            os.makedirs(save_dir, exist_ok=true)
 
        response = requests.get(url, stream=true, headers=headers)
        response.raise_for_status()
        with open(file_path, 'wb') as file:
            file.write(response.content)
        return true
    except requests.exceptions.requestexception as e:
        print(f"网络请求失败:{str(e)}")
    except ioerror as e:
        print(f"文件操作失败:{str(e)}")
    except exception as e:
        print(f"未知错误:{str(e)}")
 
    return false

使用示例

# 示例1:自动从url提取文件名
download_file(
    url="https://example.com/report.pdf",
    save_dir="./downloads"
)
 
download_file2(
    url="https://example.com/report.pdf",
    save_dir="./downloads"
)
 
# 示例2:自定义文件名
download_file(
    url="https://example.com/data?format=csv",
    save_dir="./data_files",
    file_name="dataset.csv"
)
 
download_file2(
    url="https://example.com/data?format=csv",
    save_dir="./data_files",
    file_name="dataset.csv"
)
 
# 示例3:从网络路径中query参数中获取文件名
download_file3(
    url="https://example.com/data?path=20250417\\example.csv",
    save_dir="./data_files",
    file_name="example.csv"
)

关键点说明

文件名处理:

默认从url路径中提取文件名(如https://a.com/b.zip提取b.zip)。

若url不包含文件名(如以/结尾),则使用默认名称downloaded_file。

支持通过参数file_name自定义文件名。

支持从url路径的query参数中获取文件名(如https://example.com/data?path=20250417\\example.csv提取example.csv)

路径处理:

使用os.path模块确保路径跨平台兼容。

自动创建目标目录(若不存在)。

流式下载:

使用stream=true分块下载,避免大文件占用过多内存。

通过iter_content逐块写入,提升可靠性。

异常处理:

捕获常见错误(如网络问题、权限不足)。

使用response.raise_for_status()检查http状态码。

扩展性:

支持自定义请求头(如模拟浏览器访问)。

可调整chunk_size优化下载速度与内存占用。

方法补充

python下载文件到指定文件夹

# coding:utf-8
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# print os.getcwd()
# 有些文件夹下面有很多文件夹,每个文件夹下面有很多视频文件,现在通过脚本,将文件夹下面的所有文件转移到一个目录下面
# 统计访问的文件夹数量及文件数量
countnum = [0, ]countfile = [0, ]# 选择全部移除或者指定后缀名文件
# 查找文件
def move_all_files(dir_path):
if os.path.exists(dir_path):
countnum[0] += 1
# 输出遍历的文件夹数量
print "*****", countnum[0], "*****"+dir_path
# 指定文件夹下的所有文件和文件夹
path_list = os.listdir(dir_path)
# 遍历
for each_path in path_list:
# 如果是文件夹就继续遍历
print each_path
if os.path.isdir(dir_path+""+each_path):
# 移动所有文件到指定目录下面
src=dir_path+""+each_path
move_all_files(src)
else:
# 如果是指定文件类型,则复制文件
file_type = os.path.splitext(each_path)[1] # 判断是否为选择的文件类型
selected = false
if file_type == select_type or select_type == 'all':
selected = true
if selected:
# 复制文件
src_file = dir_path + "" + each_path
des_file = des_pos + "" + each_path
print "正在复制", each_path
shutil.copyfile(src_file, des_file)
# 文件+1
countfile[0] += 1
else:
print "指定路径不存在"
# 需要复制文件的文件夹位置
give_pos = r"c:userslancedownloadsjava web编程相关"
# 需要复制到的位置
des_pos = r"c:userslancedownloads测试"
# all 或者 指定文件后缀名
select_type = 'all'
# 如果不存在,创建
if not os.path.exists(unicode(des_pos, 'utf-8')):
os.mkdir(unicode(des_pos, "utf-8"))
# 移动文件
move_all_files(unicode(give_pos, "utf-8"))
print "将文件从****'", give_pos, "'复制到****'", des_pos, "'"
print "共访问了", countnum[0], "个文件夹"
print "共复制了 ", countfile[0], " 个文件"

到此这篇关于python如何下载网络文件到本地指定文件夹的文章就介绍到这了,更多相关python下载网络文件内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com