zoukankan      html  css  js  c++  java
  • 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文件 

  • 相关阅读:
    升级系统引起的
    php curl批处理
    看《黑炮事件》想到的
    查找单链表的倒数第k个值
    python绘制树枝
    暑假第九测
    网络流24题(持续更新)
    P2657 [SCOI2009]windy数
    P3177 [HAOI2015]树上染色
    暑假第六测
  • 原文地址:https://www.cnblogs.com/xzlive/p/11810767.html
Copyright © 2011-2022 走看看