How to download a file that requires custom headers and 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
import requests | |
chunk_size = 4096 | |
filename = "logo.png" | |
document_url = "https://wasi0013.files.wordpress.com/2018/11/my_website_logo_half_circle_green-e1546027650125.png" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36", | |
"Connection": "keep-alive", | |
} | |
s = requests.Session() | |
cookie = requests.cookies.create_cookie('COOKIE_NAME','COOKIE_VALUE') | |
s.cookies.set_cookie(cookie) | |
with s.get(document_url, stream=True, headers=headers) as r: | |
with open(filename, 'wb') as f: | |
for chunk in r.iter_content(chunk_size): | |
if chunk: | |
f.write(chunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment