Python實(shí)現(xiàn)文件下載的方法匯總與適用場景介紹
在Python開發(fā)中,文件下載是常見需求。本文將全面介紹10種Python下載文件的方法,涵蓋標(biāo)準(zhǔn)庫、第三方庫以及高級技巧,每種方法都配有完整代碼示例和適用場景分析。
1. 使用urllib.request(Python標(biāo)準(zhǔn)庫)
適用場景:簡單下載需求,無需額外安裝庫
import urllib.request
url = "https://example.com/file.zip"
filename = "downloaded_file.zip"
urllib.request.urlretrieve(url, filename)
print(f"文件已保存為: {filename}")
# 進(jìn)階:添加請求頭
headers = {"User-Agent": "Mozilla/5.0"}
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as response:
with open(filename, 'wb') as f:
f.write(response.read())
2. 使用requests庫(最常用)
適用場景:需要更友好API和高級功能
import requests
url = "https://example.com/large_file.iso"
filename = "large_file.iso"
# 簡單下載
response = requests.get(url)
with open(filename, 'wb') as f:
f.write(response.content)
# 流式下載大文件
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
3. 使用wget庫
適用場景:模擬Linux wget命令行為
import wget
url = "https://example.com/image.jpg"
filename = wget.download(url)
print(f"\n下載完成: {filename}")
# 指定保存路徑
wget.download(url, out="/path/to/save/image.jpg")
4. 使用http.client(底層HTTP客戶端)
適用場景:需要底層控制或?qū)W習(xí)HTTP協(xié)議
import http.client
conn = http.client.HTTPSConnection("example.com")
conn.request("GET", "/file.pdf")
response = conn.getresponse()
with open("document.pdf", 'wb') as f:
f.write(response.read())
conn.close()
5. 使用aiohttp(異步下載)
適用場景:高性能異步下載,I/O密集型任務(wù)
import aiohttp
import asyncio
async def download_file(url, filename):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
with open(filename, 'wb') as f:
while True:
chunk = await response.content.read(8192)
if not chunk:
break
f.write(chunk)
print(f"異步下載完成: {filename}")
urls = [
("https://example.com/file1.zip", "file1.zip"),
("https://example.com/file2.zip", "file2.zip")
]
async def main():
tasks = [download_file(url, name) for url, name in urls]
await asyncio.gather(*tasks)
asyncio.run(main())
6. 使用pycurl(libcurl綁定)
適用場景:需要C級別性能或復(fù)雜傳輸選項(xiàng)
import pycurl
from io import BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, "https://example.com/data.json")
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
with open("data.json", 'wb') as f:
f.write(body)
7. 使用urllib3(requests底層庫)
適用場景:需要比requests更底層的控制
import urllib3
http = urllib3.PoolManager()
url = "https://example.com/video.mp4"
response = http.request("GET", url, preload_content=False)
with open("video.mp4", 'wb') as f:
for chunk in response.stream(1024):
f.write(chunk)
response.release_conn()
8. 使用socket原始下載(僅限高級用戶)
適用場景:學(xué)習(xí)網(wǎng)絡(luò)原理或特殊協(xié)議需求
import socket
def download_via_socket(url, port=80, filename="output.bin"):
# 解析URL(簡化版,實(shí)際應(yīng)使用urllib.parse)
host = url.split('/')[2]
path = '/' + '/'.join(url.split('/')[3:])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
request = f"GET {path} HTTP/1.1\r\nHost: {host}\r\n\r\n"
s.send(request.encode())
with open(filename, 'wb') as f:
while True:
data = s.recv(1024)
if not data:
break
f.write(data)
s.close()
???????download_via_socket("http://example.com/file")9. 使用multiprocessing多進(jìn)程下載
適用場景:CPU密集型下載任務(wù)(如需要解壓/加密)
import requests
from multiprocessing import Pool
def download(args):
url, filename = args
response = requests.get(url, stream=True)
with open(filename, 'wb') as f:
for chunk in response.iter_content(8192):
f.write(chunk)
return filename
urls = [
("https://example.com/file1.zip", "file1.zip"),
("https://example.com/file2.zip", "file2.zip")
]
with Pool(4) as p: # 4個(gè)進(jìn)程
results = p.map(download, urls)
print(f"下載完成: {results}")
10. 使用scrapy(網(wǎng)頁爬蟲下載)
適用場景:需要從網(wǎng)頁中批量下載資源
import scrapy
from scrapy.crawler import CrawlerProcess
class FileDownloadSpider(scrapy.Spider):
name = "filedownload"
start_urls = ["https://example.com/downloads"]
def parse(self, response):
for href in response.css('a.download-link::attr(href)').getall():
yield scrapy.Request(
response.urljoin(href),
callback=self.save_file
)
def save_file(self, response):
path = response.url.split('/')[-1]
with open(path, 'wb') as f:
f.write(response.body)
self.log(f"保存文件: {path}")
process = CrawlerProcess()
process.crawl(FileDownloadSpider)
process.start()
高級技巧:斷點(diǎn)續(xù)傳實(shí)現(xiàn)
import requests
import os
def download_with_resume(url, filename):
headers = {}
if os.path.exists(filename):
downloaded = os.path.getsize(filename)
headers = {'Range': f'bytes={downloaded}-'}
with requests.get(url, headers=headers, stream=True) as r:
mode = 'ab' if headers else 'wb'
with open(filename, mode) as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
download_with_resume("https://example.com/large_file.iso", "large_file.iso")
方法對比與選擇指南

安全注意事項(xiàng)
驗(yàn)證HTTPS證書:
# requests示例(默認(rèn)驗(yàn)證證書)
requests.get("https://example.com", verify=True)
限制下載大小防止DoS攻擊:
max_size = 1024 * 1024 * 100 # 100MB
response = requests.get(url, stream=True)
downloaded = 0
with open(filename, 'wb') as f:
for chunk in response.iter_content(8192):
downloaded += len(chunk)
if downloaded > max_size:
raise ValueError("文件超過最大限制")
f.write(chunk)
清理文件名防止路徑遍歷:
import re
def sanitize_filename(filename):
return re.sub(r'[\\/*?:"<>|]', "", filename)
總結(jié)
本文介紹了Python下載文件的10種方法,從標(biāo)準(zhǔn)庫到第三方庫,從同步到異步,涵蓋了各種應(yīng)用場景。選擇哪種方法取決于你的具體需求:
簡單需求:urllib.request或requests
高性能需求:aiohttp或pycurl
特殊場景:multiprocessing或scrapy
到此這篇關(guān)于Python實(shí)現(xiàn)文件下載的方法匯總與適用場景介紹的文章就介紹到這了,更多相關(guān)Python文件下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法,結(jié)合實(shí)例形式分析了Python針對中文的gbk與utf-8編碼轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
將python項(xiàng)目打包成exe與安裝包的全過程
Python唯二的難題運(yùn)行速度和源代碼反編譯,一直是被眾多語言所詬病,下面這篇文章主要給大家介紹了關(guān)于如何將python項(xiàng)目打包成exe與安裝包的相關(guān)資料,需要的朋友可以參考下2021-11-11
使用Spire.XLS for Python高效讀取Excel數(shù)據(jù)的代碼實(shí)現(xiàn)
在數(shù)據(jù)驅(qū)動(dòng)的時(shí)代,Python已成為數(shù)據(jù)處理領(lǐng)域的瑞士軍刀,然而,當(dāng)我們面對最常見的數(shù)據(jù)載體——Excel文件時(shí),如何高效、準(zhǔn)確地從中提取所需信息,卻常常成為許多開發(fā)者和數(shù)據(jù)分析師的痛點(diǎn),本文給大家介紹了如何使用Spire.XLS for Python高效讀取Excel數(shù)據(jù)2025-09-09
python實(shí)現(xiàn)人機(jī)對戰(zhàn)的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)人機(jī)對戰(zhàn)的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
基于Python實(shí)現(xiàn)個(gè)人手機(jī)定位分析
TransBigData是一個(gè)為交通時(shí)空大數(shù)據(jù)處理、分析和可視化而開發(fā)的Python包。本文就來用它實(shí)現(xiàn)個(gè)人手機(jī)定位分析,感興趣的小伙伴可以了解一下2023-04-04
使用uv快速創(chuàng)建和管理Python虛擬環(huán)境最常用的入門流程
uv是由Astral公司開發(fā)的一款Rust編寫的Python包管理器和環(huán)境管理器,它的主要目標(biāo)是提供比現(xiàn)有工具快10-100倍的性能,同時(shí)保持簡單直觀的用戶體驗(yàn),這篇文章主要介紹了使用uv快速創(chuàng)建和管理Python虛擬環(huán)境最常用的入門流程,需要的朋友可以參考下2026-01-01
matplotlib基礎(chǔ)繪圖命令之bar的使用方法
這篇文章主要介紹了matplotlib基礎(chǔ)繪圖命令之bar的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python兩個(gè)內(nèi)置函數(shù) locals 和globals(學(xué)習(xí)筆記)
這篇文章主要介紹了Python兩個(gè)內(nèi)置函數(shù) locals 和globals(學(xué)習(xí)筆記),需要的朋友可以參考下2016-08-08

