Skip to content

Instantly share code, notes, and snippets.

@jesusalber1
Last active May 9, 2016 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusalber1/51740335519fd471f313d006d1840a01 to your computer and use it in GitHub Desktop.
Save jesusalber1/51740335519fd471f313d006d1840a01 to your computer and use it in GitHub Desktop.
Download files from authenticated websites using cookies
#!/usr/bin/python
import requests
import urllib2
# Current cookies (use 'document.cookie' in Console to get them)
cookies = ''
# Create a dict from that given ookies
cookies = dict(item.split("=", 1) for item in cookies.split("; "))
# Files to be downloaded (paste them manually, use 'Copy link address'). Be careful to add ',' at the end of each line unless last
files=(
''
)
# Make it happen!
for file in files:
# Create the request and save its response
response = requests.get(file, stream=True, cookies=cookies)
# Get name of the file from its url, sometimes original url is redirected and it can be quoted
new = urllib2.unquote((response.url).rsplit('/', 1)[-1])
print('Downloading: %s' % new)
# Save each file with its own name
with open(new, 'wb') as f:
f.write(response.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment