Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a large file using the Python requests library [duplicate]

Possible Duplicate:
How to download image using requests

I know that fetching a url is as simple as requests.get and I can get at the raw response body and save it to a file, but for large files, is there a way to stream directly to a file? Like if I'm downloading a movie with it or something?

like image 920
Matt Williamson Avatar asked Jan 01 '13 22:01

Matt Williamson


People also ask

How do I download a ZIP file in Python?

Python wget download zip file One way to download a zip file from a URL in Python is to use the wget() function. But you need to install the wget library first using the pip command-line utility. Now you can use the wget library to download a zip file.


1 Answers

Oddly enough, requests doesn't have anything simple for this. You'll have to iterate over the response and write those chunks to a file:

response = requests.get('http://www.example.com/image.jpg', stream=True)  # Throw an error for bad status codes response.raise_for_status()  with open('output.jpg', 'wb') as handle:     for block in response.iter_content(1024):         handle.write(block) 

I usually just use urllib.urlretrieve(). It works, but if you need to use a session or some sort of authentication, the above code works as well.

like image 122
Blender Avatar answered Sep 28 '22 00:09

Blender