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

How to use requests to download files in python

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how to use requests to download files in python, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this python article on how to use requests to download files, let's take a look.

Use requests to download file 1, get token, or session

Ignore if you don't need it.

Login_url = "http://xxxx/api/auth/login"login_data = {" username ":" test3 "," password ":" 123456 "} login_res = requests.post (url=login_url,data = login_data) token = login_res.json () [" data "] [" token "] 2, get the download path

If the contents of the file are returned directly after the request, you can proceed to the third step directly.

Batch_url = "http://xxxx/api/models/batch"batch_data = {" ids ":" [4] "," version_number ":" [309] "} headers= {" Authorization ":" bearer% s "% token} batch_res = requests.get (url=batch_url,params=batch_data,headers=headers) 3, download url according to the download path

Complete file download and write

File_path = batch_res.json () ['data'] [' file_path'] file_name = batch_res.json () ['data'] [' file_name'] down_url = "http://xxxx/api/report/down"down_data = {" type ": 2," file_path ": file_path," file_name ": file_name "token": token} down_res = requests.get (url=down_url,params=down_data) with open (file_name, "wb") as code: code.write (down_res.content)

Note:

The second step returns the json data, including the path and file name, which is actually the file generation process. The third step downloads the files generated on the server side, and sometimes the third step cannot be seen on the page F12. You need to use the package grab tool to get it.

Download files with requests.get. I don't know if you have encountered such a problem.

That is, the url source is unstable and I download empty files from time to time. I finally come up with a good solution and share it with you.

Def downloadfile (url,filename=None): if (not filename): # if the parameter does not specify a file name filename=os.path.basename (url) # take the tail of url as the file name leng=1 while (leng==1): torrent=requests.get (url) Headers=headers) leng=len (list (torrent.iter_content (1024)) # number of download blocks if (leng==1): # if it is 1, the file is empty. Download print (filename,' download failed, re-download') sleep (1) else: print (path,' download complete') with open (filename) 'wb') as f: for chunk in torrent.iter_content (1024): # prevent the file from being too large This is the end of the article about "how to use requests to download files in python" written by f.write (chunk) in 1024 paragraphs. Thank you for reading! I believe you all have a certain understanding of "how to use requests to download files in python". If you want to learn more, you are welcome to follow the industry information channel.

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