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文件 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM