Four 、 File download
1, Ordinary download
(1) For non text requests , We can go through Response Object's content Property to access the request response body in bytes . Be careful : This mode can only download small files . Because in this mode , The data received from the website is always stored in memory , Only when write Before writing to the hard disk , If the file is large , Then the memory occupied is also very large .
(2) Next, download a picture from the network to the local and save it ( The file name does not change ):
import requests
url = 'http://www.hangge.com/blog/images/logo.png'
r = requests.get(url)
with open("logo.png", "wb") as code:
code.write(r.content)
(3) After the code runs, you can see that the image has been successfully downloaded .
2, Streaming Download
Let's change the following code to streaming download , Save while downloading . This method is suitable for downloading large files .
import requests
url = 'http://www.hangge.com/blog/images/logo.png'
r = requests.get(url, stream=True)
with open("logo.png", "wb") as f:
for bl in r.iter_content(chunk_size=1024):
if bl:
f.write(bl)
3, File download with progress
(1) If the file is large , When downloading, we'd better display the current download progress in real time . For ease of use , I have encapsulated a download method here ( Streaming download is also used internally ).
import requests
from contextlib import closing
# File downloader
def down_load(file_url, file_path):
with closing(requests.get(file_url, stream=True)) as response:
chunk_size = 1024 # Single request maximum
content_size = int(response.headers['content-length']) # The total size of the content
data_count = 0
with open(file_path, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
data_count = data_count + len(data)
now_jd = (data_count / content_size) * 100
print("\r File download progress :%d%%(%d/%d) - %s"
% (now_jd, data_count, content_size, file_path), end=" ")
if __name__ == '__main__':
fileUrl = 'http://www.hangge.com/hangge.zip' # The file link
filePath = "hangge.zip" # File path
down_load(fileUrl, filePath)
(3) The operation effect is as follows , You can see that the current progress will be displayed in real time during the file download process :
4, File download with download speed display
Here is an improvement to the above method , Increase the calculation and display of real-time download speed :
import requests
import time
from contextlib import closing
# File downloader
def down_load(file_url, file_path):
start_time = time.time() # The time when the file started downloading
with closing(requests.get(file_url, stream=True)) as response:
chunk_size = 1024 # Single request maximum
content_size = int(response.headers['content-length']) # The total size of the content
data_count = 0
with open(file_path, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
data_count = data_count + len(data)
now_jd = (data_count / content_size) * 100
speed = data_count / 1024 / (time.time() - start_time)
print("\r File download progress :%d%%(%d/%d) File download speed :%dKB/s - %s"
% (now_jd, data_count, content_size, speed, file_path), end=" ")
if __name__ == '__main__':
fileUrl = 'http://www.hangge.com/hangge.zip' # The file link
filePath = "hangge.zip" # File path
down_load(fileUrl, filePath)