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("https://autoupdate.termius.com/windows/Termius.exe") # The second step : Get the contents of the returned file , And write to local with open(r"./termius.exe", "wb") as f: f.write(r.content) 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 ：
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
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( desc=fname, total=total, unit='iB', unit_scale=True, unit_divisor=1024, ) as bar: for data in resp.iter_content(chunk_size=1024): size = file.write(data) bar.update(size) if __name__ == "__main__": # Download the file , And pass in the file name url = "https://autoupdate.termius.com/windows/Termius.exe" download("https://autoupdate.termius.com/windows/Termius.exe", "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 ..
author[Milo],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
guess what you like
Python notes (22): errors and exceptions
Python has been hidden for ten years, and once image recognition is heard all over the world
Python notes (21): random number module
Python notes (19): anonymous functions
Use Python and OpenCV to calculate and draw two-dimensional histogram
Python, Hough circle transformation in opencv
A library for reading and writing markdown in Python: mdutils
Datetime of Python time operation (Part I)
The most useful decorator in the python standard library
Python iterators and generators
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
- Parameter passing of Python function
- Stroke tuple in Python
- Talk about ordinary functions and higher-order functions in Python
- [Python data acquisition] page image crawling and saving
- [Python data collection] selenium automated test framework
- Talk about function passing and other supplements in Python
- Python programming simulation poker game
- leetcode 160. Intersection of Two Linked Lists （python）
- Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
- Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
- New statement match, the new version of Python is finally going to introduce switch case?
- Fanwai 6 Different operations for image details in Python opencv
- Python crawler native code learning (I)
- Python quantitative data warehouse building series 2: Python operation database
- Python code reading (Part 50): taking elements from list intervals
- Pyechart + pandas made word cloud pictures of 19 report documents
- [Python crawler] multithreaded daemon & join() blocking
- Python crawls cat pictures in batches to realize thousand image imaging
- Van * Python | simple crawling of a planet
- Input and output of Python practice