Skip to content

Instantly share code, notes, and snippets.

@jmorenoamor
Created January 4, 2013 10:42
Show Gist options
  • Save jmorenoamor/4451588 to your computer and use it in GitHub Desktop.
Save jmorenoamor/4451588 to your computer and use it in GitHub Desktop.
Download a remote resource using python and requests
# All the magic comes from the requests library http://www.python-requests.org
import requests
# URI of the resource we want to download
FILE_URI = 'http://www.example.com'
# Path to the local file where we will save the remote resource
FILE_NAME = 'downloaded_file.html'
# Chunk size that we will keep in memory, in bytes
CHUNK_SIZE = 1 * 1024
# Create the requests object, but only get the headers
r = requests.get(FILE_URI, stream=True)
# We can access the headers for information
size = int(r.headers['Content-Length'].strip())
print "Downloading %d bytes to %s" % (size, FILE_NAME)
print r.encoding
print r.status_code
# Open the file and start iterating and writing the contents to te file
with open(FILE_NAME, 'w') as file:
for chunk in r.iter_content(CHUNK_SIZE):
if chunk:
file.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment