Python3使用requests模塊實現(xiàn)顯示下載進度的方法詳解
本文實例講述了Python3使用requests模塊實現(xiàn)顯示下載進度的方法。分享給大家供大家參考,具體如下:
一、配置request
1. 相關資料
請求關鍵參數(shù):stream=True
。默認情況下,當你進行網絡請求后,響應體會立即被下載。你可以通過 stream 參數(shù)覆蓋這個行為,推遲下載響應體直到訪問 Response.content
屬性。
tarball_url = 'https://github.com/kennethreitz/requests/tarball/master' r = requests.get(tarball_url, stream=True)
此時僅有響應頭被下載下來了,連接保持打開狀態(tài),因此允許我們根據條件獲取內容:
if int(r.headers['content-length']) < TOO_LONG: content = r.content ...
進一步使用 Response.iter_content
和 Response.iter_lines
方法來控制工作流,或者以 Response.raw
從底層urllib3的 urllib3.HTTPResponse
from contextlib import closing with closing(requests.get('http://httpbin.org/get', stream=True)) as r: # Do things with the response here.
保持活動狀態(tài)(持久連接)
歸功于urllib3,同一會話內的持久連接是完全自動處理的,同一會話內發(fā)出的任何請求都會自動復用恰當?shù)倪B接!
注意:只有當響應體的所有數(shù)據被讀取完畢時,連接才會被釋放到連接池;所以確保將 stream 設置為 False 或讀取 Response 對象的 content 屬性。
2. 下載文件并顯示進度條
with closing(requests.get(self.url(), stream=True)) as response: chunk_size = 1024 # 單次請求最大值 content_size = int(response.headers['content-length']) # 內容體總大小 progress = ProgressBar(self.file_name(), total=content_size, unit="KB", chunk_size=chunk_size, run_status="正在下載", fin_status="下載完成") with open(file_name, "wb") as file: for data in response.iter_content(chunk_size=chunk_size): file.write(data) progress.refresh(count=len(data))
二、進度條類的實現(xiàn)
在Python3中,print()方法的默認結束符(end='\n'),當調用完之后,光標自動切換到下一行,此時就不能更新原有輸出。
將結束符改為“\r”,輸出完成之后,光標會回到行首,并不換行。此時再次調用print()方法,就會更新這一行輸出了。
結束符也可以使用“\d”,為退格符,光標回退一格,可以使用多個,按需求回退。
在結束這一行輸出時,將結束符改回“\n”或者不指定使用默認
下面是一個格式化的進度條顯示模塊。代碼如下:
class ProgressBar(object): def __init__(self, title, count=0.0, run_status=None, fin_status=None, total=100.0, unit='', sep='/', chunk_size=1.0): super(ProgressBar, self).__init__() self.info = "【%s】%s %.2f %s %s %.2f %s" self.title = title self.total = total self.count = count self.chunk_size = chunk_size self.status = run_status or "" self.fin_status = fin_status or " " * len(self.status) self.unit = unit self.seq = sep def __get_info(self): # 【名稱】狀態(tài) 進度 單位 分割線 總數(shù) 單位 _info = self.info % (self.title, self.status, self.count/self.chunk_size, self.unit, self.seq, self.total/self.chunk_size, self.unit) return _info def refresh(self, count=1, status=None): self.count += count # if status is not None: self.status = status or self.status end_str = "\r" if self.count >= self.total: end_str = '\n' self.status = status or self.fin_status print(self.__get_info(), end=end_str)
三、參考資料
http://cn.python-requests.org/en/latest/user/advanced.html
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數(shù)據結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
- 淺析Python requests 模塊
- Python使用requests模塊爬取百度翻譯
- Python grequests模塊使用場景及代碼實例
- Python requests模塊安裝及使用教程圖解
- Python requests模塊cookie實例解析
- python爬蟲開發(fā)之Request模塊從安裝到詳細使用方法與實例全解
- Python3離線安裝Requests模塊問題
- python爬蟲 基于requests模塊的get請求實現(xiàn)詳解
- python爬蟲 基于requests模塊發(fā)起ajax的get請求實現(xiàn)解析
- python利用re,bs4,requests模塊獲取股票數(shù)據
- Python實現(xiàn)使用request模塊下載圖片demo示例
- python request 模塊詳細介紹
相關文章
手把手教你搭建python+selenium自動化環(huán)境(圖文)
本文主要介紹了手把手教你搭建python+selenium自動化環(huán)境,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06Python虛擬環(huán)境的創(chuàng)建和使用詳解
這篇文章主要給大家介紹了關于Python虛擬環(huán)境的創(chuàng)建和使用的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Django 實現(xiàn)將圖片轉為Base64,然后使用json傳輸
這篇文章主要介紹了Django 實現(xiàn)將圖片轉為Base64,然后使用json傳輸,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03使用keras實現(xiàn)densenet和Xception的模型融合
這篇文章主要介紹了使用keras實現(xiàn)densenet和Xception的模型融合,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05