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("\r 文件下载进度: %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文件 

 

posted @ 2019-11-07 11:03  MR__Wang  阅读(1012)  评论(0编辑  收藏  举报