current position:Home>Python shows the progress of downloading files from requests

Python shows the progress of downloading files from requests

2022-01-31 00:10:51 Milo

This is my participation 11 The fourth of the yuegengwen challenge 3 God , Check out the activity details :2021 One last more challenge


Everyone in Python When writing some small programs , Often used File download , For smaller files , You may not care much about the download progress of the file , Because it will be downloaded soon .

But when the file is large , For example, download. chromedriver When , If we can see the download progress bar , How friendly that should be . After all, npm,pip When installing packages, there are similar Progress bar .

Today, I will share with you a method to show the file download progress bar .


requests Ku believes everyone has used , It is indispensable for interface testing . In fact, we usually download files , It can also be used. requests Do it , For example, there is such an address :…

We're going to download it , What should be done ?

Let's write pseudo code here , Very easy to understand .

#  First step :  Visit this link 
import requests

r = requests.get("")

#  The second step :  Get the contents of the returned file , And write to local 

with open(r"./termius.exe", "wb") as f:

 Copy code 

Because my file is very big , So the process is long , I won't show the details here . You can find a few M Try a small file . Put the final result :

 You can see , completion of enforcement , The file was also obtained


Do you think , Downloading files like this is tedious , I don't know the progress , For example, how much has been downloaded , Especially for larger files , I thought he was disconnected .

If we can download the file , How nice to show the progress bar , such as thunderbolt / Baidu SkyDrive Like that .

Just install one library

To do this , We just need to install tqdm Just the library .

pip install tqdm
 Copy code 

Here is the annotated sample code :

import requests
from tqdm import tqdm

def download(url: str, fname: str):
    #  Using stream stream Way to obtain url The data of 
    resp = requests.get(url, stream=True)
    #  Get the length of the file , And put total Initialize to 0
    total = int(resp.headers.get('content-length', 0))
    #  Open the fname file ( Name you pass in )
    #  initialization tqdm, Total incoming , File name and other data , Then write , Update and so on 
    with open(fname, 'wb') as file, tqdm(
    ) as bar:
        for data in resp.iter_content(chunk_size=1024):
            size = file.write(data)

if __name__ == "__main__":
    #  Download the file , And pass in the file name 
    url = ""
    download("", "haha.exe")
 Copy code 

Simple code ,tqdm The content of is basically written in a fixed way , No doubt , Pay attention to requests Just wait for the familiar part .

Let's see gif design sketch , Is it crazy to show off cool bully and blow up the sky ..

copyright notice
author[Milo],Please bring the original link to reprint, thank you.

Random recommended