python 下载文件
在 Python 中下载文件可以通过多种方式实现,以下是 6 种常见方法,涵盖标准库和第三方库,适用于不同场景:
1. 使用 urllib.request(Python 内置库)
python
import urllib.request
url = "https://example.com/file.zip"
save_path = "downloaded_file.zip"
urllib.request.urlretrieve(url, save_path)
print(f"文件已保存到: {save_path}")
特点:
无需安装第三方库,适合简单下载。
不支持断点续传和复杂请求头。
2. 使用 requests 库(推荐)
python
import requests
url = "https://example.com/file.zip"
save_path = "downloaded_file.zip"
response = requests.get(url, stream=True)
with open(save_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print("下载完成!")
优势:
支持 分块下载(大文件友好)。
可添加请求头、代理、Cookie 等。
安装:pip install requests
3. 下载大文件(带进度条)
结合 tqdm 显示下载进度:
python
import requests
from tqdm import tqdm
url = "https://example.com/large_file.iso"
save_path = "large_file.iso"
response = requests.get(url, stream=True)
total_size = int(response.headers.get("content-length", 0))
with open(save_path, "wb") as f, tqdm(
desc=save_path,
total=total_size,
unit="B",
unit_scale=True,
) as bar:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
bar.update(len(chunk))
效果:
large_file.iso: 100%|█████████| 256M/256M [00:10<00:00, 25.6MB/s]
4. 异步下载(aiohttp + asyncio)
适合批量高速下载:
python
import aiohttp
import asyncio
async def download_file(url, save_path):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
with open(save_path, "wb") as f:
while True:
chunk = await response.content.read(8192)
if not chunk:
break
f.write(chunk)
urls = ["https://example.com/file1.zip", "https://example.com/file2.zip"]
tasks = [download_file(url, f"file_{i}.zip") for i, url in enumerate(urls)]
asyncio.run(asyncio.gather(*tasks))
安装:pip install aiohttp
5. 断点续传
python
import os
import requests
url = "https://example.com/big_file.zip"
save_path = "big_file.zip"
if os.path.exists(save_path):
downloaded_size = os.path.getsize(save_path)
else:
downloaded_size = 0
headers = {"Range": f"bytes={downloaded_size}-"}
response = requests.get(url, headers=headers, stream=True)
mode = "ab" if downloaded_size else "wb"
with open(save_path, mode) as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
适用场景:
网络不稳定时恢复下载。
6. 下载需要认证的文件
python
import requests
url = "https://example.com/protected/file.zip"
save_path = "file.zip"
auth = ("username", "password") # 或使用 API Key
response = requests.get(url, auth=auth)
with open(save_path, "wb") as f:
f.write(response.content)
总结
方法 适用场景 优点
urllib.request 快速简单下载 无需安装第三方库
requests 通用下载(推荐) 功能全面,支持分块下载
tqdm + requests 大文件下载带进度条 直观显示进度
aiohttp 高性能异步下载 适合批量任务
断点续传 网络不稳定时恢复下载 避免重复下载
认证下载 需要登录/API 密钥的文件 支持 Basic Auth/OAuth
推荐选择:
大多数场景用 requests + tqdm(平衡易用性与功能)。
需要高性能时选 aiohttp。

