首页 > 其他 > 详细

requests模块下载视频 显示进度和网速

时间:2019-05-20 17:34:57      阅读:206      评论:0      收藏:0      [点我收藏+]

requests 下载视频

import os,time
import requests

def downloadFile(name, url):
    headers = {Proxy-Connection: keep-alive}
    r = requests.get(url, stream=True, headers=headers)
    length = float(r.headers[content-length])
    f = open(name, wb)
    count = 0
    count_tmp = 0
    time1 = time.time()
    for chunk in r.iter_content(chunk_size=512):
        if chunk:
            f.write(chunk)
            count += len(chunk)
            if time.time() - time1 > 2:
                p = count / length * 100
                speed = (count - count_tmp) / 1024  / 2
                count_tmp = count
                print(name + :  + formatFloat(p) + % +  Speed:  + formatFloat(speed) + KB/S)
                time1 = time.time()
    f.close()


def formatFloat(num):
    return {:.2f}.format(num)


if __name__ == __main__:
    downloadFile(movie6.mp4, http://download.xunleizuida.com/1905/神秘慈善家.BD1280高清中字版.mp4)

 

requests模块下载视频 显示进度和网速

原文:https://www.cnblogs.com/liruixin/p/10895141.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!