Example Analysis of requests File download in Python3
This article mainly introduces the example analysis of downloading requests files in Python3, which is very detailed and has a certain reference value. Interested friends must finish reading it!
Example:
"" use module threading to download network resources # realize file download, display file information & download progress # console runs to show progress "" import requestsimport os.path as opimport osfrom sys import stdoutdef downloadfile (url, filename): "" download file and display process: param url: resource address: param filename: saved name Save in the current directory "" # 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: # only the response header is downloaded # print (r.headers) print ("download File basic Information:") print ('-'* 30) print ("File name:", filename) print ("File Type:", r.headers ["Content-Type"]) filesize = r.headers ["Content-Length"] print ("File size:", filesize "bytes") print ("download address:" url) print ("Save path:" File_to_save) print ('-'* 30) print ("start downloading") chunk_size = 128times = 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