开发者

Python下载文件的10种方法介绍

目录
  • 1. 使用urllib.request模块
  • 2. 使用requests库(简单方式)
  • 3. 使用requests库(带进度条)
  • 4. 使用wget库
  • 5. 使用http.client(标准库)
  • 6. 使用aiohttp(异步下载)
  • 7. 使用pycurl(高性能下载)
  • 8. 使用urllib3库
  • 9. 使用ftplib下载FTP文件
  • 10. 使用scp下载(通过paramiko)
  • 总结

在python中,我们可以使用多种方法来实现文件下载功能。本文将介绍10种不同的Python文件下载方式,并提供详细的代码示例。

1. 使用urllib.request模块

import urllib.request

urljs = 'https://example.com/file.zip'
filename = 'file.zip'

urllib.request.urlretrieve(url, filename)
print(f"文件已下载到: {filename}")

2. 使用requests库(简单方式)

import requests

url = 'https://example.com/file.zip'
filename = 'file.zip'

response = requests.get(url)
with open(filename, 'wb') as f:
    f.write(response.content)
print("下载完成!")

3. 使用requests库(带进度条)

import requests
from t编程客栈qdm import tqdm

url = 'https://example.com/largefile.zip'
filename = 'large编程客栈file.zip'

response = requests.get(url, stream=True)
total_size = int(response.headers.get('content-length', 0))
block_size = 1024php  # 1KB

progress_bar = tqdm(total=total_size, unit='iB', unit_scale=True)
with open(filename, 'wb') as f:
    for data in response.iter_content(block_size):
        progress_bar.update(len(data))
        f.write(data)
progress_bar.close()
print("\n下载完成!")

4. 使用wget库

import wget

url = 'https://example.com/file.zip'
filename = wget.download(url)
print(f"\n文件已下载到: {filename}")

5. 使用http.client(标准库)

import http.client
import urllib.parse

url = 'https://example.com/file.zip'
parsed_url = urllib.parse.urlparse(url)
filename = 'file.zip'

conn = http.client.HTTPSConnection(parsed_url.netloc)
conn.request("GET", parsed_url.path)
response = conn.getresponse()

with open(filename, 'wb') as f:
    f.write(response.read())
conn.close()
print("下载完成!")

6. 使用aiohttp(异步下载)

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(1024)
                    if not chunk:
                        break
                    f.write(chunk)
    print(f"文件已下载到: {filename}")

url = 'https://example.com/file.zip'
filename = 'file.zip'

​​​​​​​asyncio.run(download_file(url, filename))

7. 使用pycurl(高性能下载)

import pycurl
from io import BytesIO

url = 'https://example.com/file.zip'
filename = 'file.zip'

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

with open(filename, 'wb') as f:
    f.write(buffer.getvalue())
print("下载完成!")

8. 使用urllib3库

import urllib3

url = 'https://example.com/file.zip'
filename = 'file.zip'

http = urllib3.PoolManager()
response = http.request('GET', url)

with open(filename, 'wb') as f:
    f.write(response.data)
print("下载完成!")

9. 使用ftplib下载FTP文件

from ftplib import FTP

ftp = FTP('ftp.example.com')
ftp.login(user='username', passwd='password')
filename = 'remote_file.zip'
local_filename = 'local_file.zip'

with open(local_filename, 'wb') as f:
    ftp.retrbinary(f'RETR {filename}', f.write)
ftp.quit()
print("FTP文件下载完成!")

10. 使用scp下载(通过paramiko)

import paramiko

host = 'example.com'
port = 22
username = 'user'
password = 'password'
remote_path = '/path/to/remote/file.zip'
local_path = 'file.zip'

ssh = paramiko.SSHClient()
ssh.set_missiNIOiung_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)

sftp = ssh.open_sftp()
sftp.get(remote_path, local_path)
sftp.close()
ssh.close()
print("SCP文件下载完成!")

总结

本文介绍了10种Python下载文件的方法,包括:

  • 1.urllib.request标准库方法
  • 2.requests库简单方法
  • 3.requests库带进度条方法
  • 4.wget库专用方法
  • 5.http.client标准库方法
  • 6.aiohttp异步方法
  • 7.pycurl高性能方法
  • 8.urllib3库方法
  • 9.ftplib FTP下载
  • 10.paramiko SCP下载

根据不同的需求和场景,可以选择最适合的方法。对于简单的下载任务,requests库是最方便的选择;对于大文件下载,带进度条的requests或异步aiohttp更合适;而对于特殊协议如FTP/SCP,则需要使用专门的库。

到此这篇关于Python下载文件的10种方法介绍的文章就介绍到这了,更多相关Python下载文件内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜