python 下载文件进度条

进度条

import time
import requests

def downloader(url, path, title):
    start = time.time()
    size = 0
    res = requests.get(url, stream=True)

    chunk_size = 1024 # 每次下载数据大小
    content_size = int(res.headers["content-length"]) # 总大小
    if res.status_code == 200:
        print('[%s 文件大小]: %0.2f MB' % (title, content_size/chunk_size/1024))
        with open(path, 'wb') as f:
            for data in res.iter_content(chunk_size=chunk_size):
                f.write(data)
                size += len(data)  # 已下载文件大小
                # \r 指定第一个字符开始,搭配end属性完成覆盖进度条
                print('\r'+ '[下载进度]: %s%.2f%%' % ('>'*int(size*50/content_size), float(size/content_size*100)), end='')
        end = time.time()
        print('\n' + "全部下载完成!用时%s.2f秒" % (end - start))

if __name__ == '__main__':


   downloader(url=dic.get("url"), path=f"{dic.get('title')}.mp4", title=dic.get('title'))

在这里插入图片描述

你可能感兴趣的:(p爬虫)