• python3 爬虫利用Requests 实现下载进度条


     一、编写代码

    from datetime import datetime,date,timedelta
    from contextlib import closing
    import urllib,urllib3
    import os 
    import requests
    
    
    def downLoad(fileUrl,filePath):
        headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
        with closing(requests.get(fileUrl,headers=headers,stream=True)) as response:
            chunkSize = 1024
            contentSize = int(response.headers['content-length'])
            dateCount = 0
            with open(filePath,"wb") as file:
                for data in response.iter_content(chunk_size=chunkSize):
                    file.write(data)
                    dateCount = dateCount + len(data)
                    nowJd = (dateCount / contentSize) * 100
                    print("
     文件下载进度: %d%%(%d%d) - %s" % (nowJd,dateCount,contentSize,filePath),end='')
    
    def getUrl(**args):
        yesterday=(date.today() + timedelta(days = -2)).strftime("%Y-%m-%d")
        fileName='xxx-'+ yesterday +'.rar'
        url = "http://3.1.2.2:8079/"+ fileName
        return url,fileName
        
    
    
    if __name__ == "__main__":
        headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
        fileUrl = getUrl()[0]
        filePath = "D:downfile\"+ getUrl()[1]
        downLoad(fileUrl,filePath)
    

    二、pyinstaller 打包为exe文件

    pyinstaller .downfile.py
    

    三、运行exe文件 

  • 相关阅读:
    大数据下高并发的处理详解
    【玩转TensorFlow】TensorFlow常见问题详解
    在阿里云上两分钟玩转AlextNet
    【前端精华】React源码分析系列
    svm
    神经网络结构选择
    神经网络反向传播跳出局部极小
    ubuntu16.04设置电池充电阈值
    pandas datafram重命名列名称
    centos6.8/ubuntu 安装python2.7 or python3.6
  • 原文地址:https://www.cnblogs.com/xzlive/p/11810767.html
Copyright © 2020-2023  润新知