• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:python-下载大文件

python 搞代码 3年前 (2022-02-20) 20次浏览 已收录 0个评论
文章目录[隐藏]

1, 下载,必定要看进度

优良的 progress bar

tqdm

装置

sudo python3 -m pip install tqdm

1.1, 加环境变量

#!/usr/bin/python3
from tqdm import tqdm

import requests

上一步装置 tqdm 的是 python3 ,
python3 关联到了 tqdm 这个库,

不加环境变量,
走默认的 python 2.7,

不是在 python 2.7 上,装置的这个库 tqdm

2 , 下载代码

这个文件,700 M 左右
走文件流,下载的速度,比 Chrome 快多了
走 Chrome 下载,老是失败

#!/usr/bin/python3
from tqdm import tqdm

import requests

url = 'https://static.realm.io/downloads/swift/realm-swift-10.1.1.zip'

# Streaming, so we can iterate over the response.
response = requests.get(url, stream=True)
total_size_in_bytes = int(response.headers.get('content-length', 0))
block_size = 1024 #1 Kibibyte
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)


path = '/Users/xx/Desktop/Papr-develop/realm-swift-10.1.1.zip'

print("总量:")

print(total_size_in_bytes)

with open(path, 'wb') as file:
    for data in response.iter_content(block_size):
        progress_bar.update(len(data))
        file.write(data)


progress_bar.close()


if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
    print("ERROR, something went wrong")

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:python-下载大文件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址