Python3 requests文件下載 期間顯示文件信息和下載進(jìn)度代碼實(shí)例
這篇文章主要介紹了Python3 requests文件下載 期間顯示文件信息和下載進(jìn)度代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
"""使用模塊線程方式實(shí)現(xiàn)網(wǎng)絡(luò)資源的下載
# 實(shí)現(xiàn)文件下載, 期間顯示文件信息&下載進(jìn)度
# 控制臺運(yùn)行以顯示進(jìn)度
"""
import requests
import os.path as op
import os
from sys import stdout
def downloadfile(url, filename):
"""下載文件并顯示過程
:param url: 資源地址
:param filename: 保存的名字, 保存在當(dāng)前目錄
"""
# print(url)
filename = filename + '.' + op.splitext(url)[-1]
file_to_save = op.join(os.getcwd(), filename)
# print(file_to_save)
with open(file_to_save, "wb") as fw:
with requests.get(url, stream=True) as r:
# 此時(shí)只有響應(yīng)頭被下載
# print(r.headers)
print("下載文件基本信息:")
print('-' * 30)
print("文件名稱:", filename)
print("文件類型:", r.headers["Content-Type"])
filesize = r.headers["Content-Length"]
print("文件大小:", filesize, "bytes")
print("下載地址:", url)
print("保存路徑:", file_to_save)
print('-' * 30)
print("開始下載")
chunk_size = 128
times = int(filesize) // chunk_size
show = 1 / times
show2 = 1 / times
start = 1
for chunk in r.iter_content(chunk_size):
fw.write(chunk)
if start <= times:
stdout.write(f"下載進(jìn)度: {show:.2%}\r")
start += 1
show += show2
else:
stdout.write("下載進(jìn)度: 100%")
print("\n結(jié)束下載")
if __name__ == "__main__":
downloadfile("https://code.jquery.com/jquery-3.4.1.js", "a")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析
這篇文章主要介紹了python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
解決Python import docx出錯(cuò)DLL load failed的問題
今天小編就為大家分享一篇解決Python import docx出錯(cuò)DLL load failed的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例
這篇文章主要介紹了python基于queue和threading實(shí)現(xiàn)多線程下載實(shí)例,是比較實(shí)用的技巧,需要的朋友可以參考下2014-10-10
python基礎(chǔ)教程之基本內(nèi)置數(shù)據(jù)類型介紹
在Python程序中,每個(gè)數(shù)據(jù)都是對像,每個(gè)對像都有自己的一個(gè)類型。不同類型有不同的操作方法,使用內(nèi)置數(shù)據(jù)類型獨(dú)有的操作方法,可以更快的完成很多工作2014-02-02
python實(shí)現(xiàn)按行切分文本文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)按行切分文本文件的方法,涉及Python利用shell命令操作文本文件的相關(guān)技巧,需要的朋友可以參考下2016-04-04

