Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of requests File download in Python3

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report