Last active
May 9, 2016 20:07
-
-
Save jesusalber1/51740335519fd471f313d006d1840a01 to your computer and use it in GitHub Desktop.
Download files from authenticated websites using cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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