python 下载文件的多种方法汇总

作者:Python技术 时间:2023-08-11 16:50:05 

本文档介绍了 Python 下载文件的各种方式,从下载简单的小文件到用断点续传的方式下载大文件。

Requests

使用 Requests 模块的 get 方法从一个 url 上下载文件,在 python 爬虫中经常使用它下载简单的网页内容


import requests

# 图片来自bing.com
url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def requests_download():

content = requests.get(url).content

with open('pic_requests.jpg', 'wb') as file:
   file.write(content)

urllib

使用 python 内置的 urllib 模块的 urlretrieve 方法直接将 url 请求保存成文件


from urllib import request

# 图片来自bing.com
url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def urllib_download():
 request.urlretrieve(url, 'pic_urllib.jpg')

urllib3

urllib3 是一个用于 Http 客户端的 Python 模块,它使用连接池对网络进行请求访问


def urllib3_download():
 # 创建一个连接池
 poolManager = urllib3.PoolManager()

resp = poolManager.request('GET', url)

with open("pic_urllib3.jpg", "wb") as file:
   file.write(resp.data)

resp.release_conn()

wget

在 Linux 系统中有 wget 命令,可以方便的下载网上的资源,Python 中也有相应的 wget 模块。使用 pip install 命令安装


import wget

# 图片来自bing.com
url = 'https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg'

def wget_download():
 wget.download(url, out='pic_wget.jpg')

也可以直接在命令行中使用 wget 命令


python -m wget https://cn.bing.com/th?id=OHR.DerwentIsle_EN-CN8738104578_400x240.jpg

分块下载大文件

在需要下载的文件非常大,电脑的内存空间完全不够用的情况下,可以使用 requests 模块的流模式,默认情况下 stream 参数为 False, 文件过大会导致内存不足。stream 参数为 True 的时候 requests 并不会立刻开始下载,只有在调用 iter_content 或者 iter_lines 遍历内容时下载

iter_content:一块一块的遍历要下载的内容 iter_lines:一行一行的遍历要下载的内容


import requests

def steam_download():
 # vscode 客户端
 url = 'https://vscode.cdn.azure.cn/stable/e5a624b788d92b8d34d1392e4c4d9789406efe8f/VSCodeUserSetup-x64-1.51.1.exe'

with requests.get(url, stream=True) as r:
   with open('vscode.exe', 'wb') as flie:
     # chunk_size 指定写入大小每次写入 1024 * 1024 bytes
     for chunk in r.iter_content(chunk_size=1024*1024):
       if chunk:
         flie.write(chunk)

进度条

在下载大文件的时候加上进度条美化下载界面,可以实时知道下载的网络速度和已经下载的文件大小,这里使用 tqdm 模块作为进度条显示,可以使用 pip install tqdm 安装


from tqdm import tqdm

def tqdm_download():

url = 'https://vscode.cdn.azure.cn/stable/e5a624b788d92b8d34d1392e4c4d9789406efe8f/VSCodeUserSetup-x64-1.51.1.exe'

resp = requests.get(url, stream=True)

# 获取文件大小
 file_size = int(resp.headers['content-length'])

with tqdm(total=file_size, unit='B', unit_scale=True, unit_divisor=1024, ascii=True, desc='vscode.exe') as bar:
   with requests.get(url, stream=True) as r:
     with open('vscode.exe', 'wb') as fp:
       for chunk in r.iter_content(chunk_size=512):
         if chunk:
           fp.write(chunk)
           bar.update(len(chunk))

tqdm 参数说明:

  1. total:bytes,整个文件的大小

  2. unit='B': 按 bytes 为单位计算

  3. unit_scale=True:以 M 为单位显示速度

  4. unit_divisor=1024:文件大小和速度按 1024 除以,默认时按 1000 来除

  5. ascii=True:进度条的显示符号,用于兼容 windows 系统

  6. desc='vscode.exe' 进度条前面的文件名

示例结果

python 下载文件的多种方法汇总

断点续传

HTTP/1.1 在协议的请求头中增加了一个名为 Range的字段域, Range 字段域让文件从已经下载的内容开始继续下载

如果网站支持 Range 字段域请求响应的状态码为 206(Partial Content),否则为 416(Requested Range not satisfiable)

Range 的格式


Range:[unit=first byte pos] - [last byte pos],即 Range = 开始字节位置-结束字节位置,单位:bytes

将 Range 加入到 headers 中


from tqdm import tqdm

def duan_download():
 url = 'https://vscode.cdn.azure.cn/stable/e5a624b788d92b8d34d1392e4c4d9789406efe8f/VSCodeUserSetup-x64-1.51.1.exe'

r = requests.get(url, stream=True)

# 获取文件大小
 file_size = int(r.headers['content-length'])

file_name = 'vscode.exe'
 # 如果文件存在获取文件大小,否在从 0 开始下载,
 first_byte = 0
 if os.path.exists(file_name):
   first_byte = os.path.getsize(file_name)

# 判断是否已经下载完成
 if first_byte >= file_size:
   return

# Range 加入请求头
 header = {"Range": f"bytes={first_byte}-{file_size}"}

# 加了一个 initial 参数
 with tqdm(total=file_size, unit='B', initial=first_byte, unit_scale=True, unit_divisor=1024, ascii=True, desc=file_name) as bar:
   # 加 headers 参数
   with requests.get(url, headers = header, stream=True) as r:
     with open(file_name, 'ab') as fp:
       for chunk in r.iter_content(chunk_size=512):
         if chunk:
           fp.write(chunk)
           bar.update(len(chunk))

示例结果

启动下载一段时间后,关闭脚本重新运行,文件在断开的内容后继续下载

python 下载文件的多种方法汇总

总结

本文介绍了常用的 7 中文件下载方式,其他的下载方式大家可以在留言区交流交流共同进步

示例代码:Python 下载文件的多种方法

来源:http://www.justdopython.com/2020/11/16/python-downloadFile/?utm_source=tuicool&utm_medium=referral

标签:python,下载
0
投稿

猜你喜欢

  • FCKEDITOR 的高级功能和常见问题的解决方法

    2023-12-16 16:07:13
  • OpenCV半小时掌握基本操作之直线检测

    2023-12-27 12:31:29
  • 一文带你掌握Go语言中的文件读取操作

    2024-05-21 10:24:57
  • php文件上传类完整实例

    2023-11-22 04:32:28
  • python 中的9个实用技巧,助你提高开发效率

    2021-05-01 08:26:25
  • Javascript中常见的逻辑题和解决方法

    2024-05-22 10:31:21
  • 整理MySql常用查询语句(23种)

    2024-01-23 09:14:46
  • 详解Vue中使用v-for语句抛出错误的解决方案

    2024-04-09 10:44:34
  • php事务处理实例详解

    2024-05-13 09:25:30
  • 利用python将图片版PDF转文字版PDF

    2021-07-20 21:22:25
  • Python小工具之消耗系统指定大小内存的方法

    2022-10-19 18:53:51
  • Mysql带And关键字的多条件查询语句

    2024-01-14 08:41:17
  • Selenium常见异常解析及解决方案示范

    2023-06-27 20:09:48
  • golang实现unicode转换为字符串string的方法

    2024-05-09 09:30:44
  • MySQL中datetime时间字段的四舍五入操作

    2024-01-28 08:00:40
  • MySQL定位并优化慢查询sql的详细实例

    2024-01-25 20:32:16
  • bootstrap 通过加减按钮实现输入框组功能

    2024-04-16 09:26:06
  • 浅谈webpack打包过程中因为图片的路径导致的问题

    2024-04-28 09:42:38
  • 浅谈Python中的私有变量

    2023-07-21 17:59:46
  • 解决mysql ERROR 1045 (28000)-- Access denied for user问题

    2024-01-24 01:30:43
  • asp之家 网络编程 m.aspxhome.com