Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chidindu-ogbonna/20826f509e8f36f9a54e433eae0d2538 to your computer and use it in GitHub Desktop.

Select an option

Save chidindu-ogbonna/20826f509e8f36f9a54e433eae0d2538 to your computer and use it in GitHub Desktop.
Downloading a file with the requests module
import requests
import shutil
import json
# Writing a json response from an API to file
r = requests.get('http://api.domain.com')
with open('file.json', 'w') as f:
json.dump(r.json(), f, indent=2)
# Writing a csv from a response to file
response = requests.get('http://www.football-data.co.uk/mmz4281/1718/E0.csv') # Note this is a csv url (contains the file)
with open('file.csv', 'wb') as f:
f.write(response.content)
# For a large file
response = requests.get('https://www.example.com', stream=True)
with open('file.csv', 'wb') as f:
for chunk in response.iter_content(chunk_size): # the chunk size (int) is the size you want to save as e.g 2000
f.write(chunk)
# For images
response = requests.get(settings.STATIC_MAP_URL.format(**data), stream=True) # stream is true, so it does not download into memory first
if response.status_code == 200:
with open(path, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
# This could also be good for images
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
with open(path, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment