1

I have a download link like such:

url = https://api.some.service:443/v2/encodedreport/13402/custom/7378/2019/05/30/MTA4Nzk2X1RlYWRzJTIwLSUyMERTVyUyMFJlcG9ydF8yMDE5MDUzMDEzMDU1OC54bHN4

When i click on the link or open in a browser it starts the download manager and I can click download to download the (xlsx) file. I'd like to automate this download with python.

I tried using requests:

>>> r = requests.get(url)
# long string of binary looking data
>>> r.content
b'PK\x03\x04\x14\x00\x08\x08\x08\x00\xbdh\xbeN\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00_rels/.rels\xad\x92MK\x031\x10\x86\xffJ\x98{7\xdb*"\xd2\xb4\x17\x11z\x13\xa9?`Lf?\xd8M&$S]\xff\xbdA\x0f\xda\xb2\x85\n=\x0e3\xef\xf3>\x87Yo\'?\xaawJ\xb9\xe7``Y\xd5\xa0(Xv}h\r\xbc\xee\x9f\x16\xf7\xa0\xb2`p8r \x03\x81a\xbbY\xbf\xd0\x8...
# seems to involve aws s3
>>> r.headers
{'x-amz-id-2': 'lI0SDLtoaJHSJF4Ekn6uq81aFNn...f5rGbREk3iEKzZSq2Hyr1ykFwOYq2kpw=', 'x-amz-request-id': 'C01C13...C19B3C', 'Date': 'Mon, 01 Jul 2019 13:59:45 GMT', 'Last-Modified': 'Thu, 30 May 2019 13:05:59 GMT', 'x-amz-expiration': 'expiry-date="Sun, 30 May 2021 00:00:00 GMT", rule-id="outdated-reports-after-2years"', 'ETag': '"3924c5ba183c...88bf658db37dc"', 'x-amz-server-side-encryption': 'AES256', 'Accept-Ranges': 'bytes', 'Content-Type': 'application/octet-stream', 'Content-Length': '8261', 'Server': 'AmazonS3'}

How can i download this file programmatically?

RSHAP
  • 2,337
  • 3
  • 28
  • 39
  • 1
    I believe you have already downloaded this file. `r.content` shows this file. All you need is just storing it like `with open("whatever.xlsx", "w") as f: \n f.write(r.content)`. – Sraw Jul 01 '19 at 14:47
  • lmao answer is right in front of my face. It's an excel file so that's just what it looks like. Feel free to answer and I'll mark it. – RSHAP Jul 01 '19 at 14:52

1 Answers1

3
r = requests.get(url, allow_redirects=True)
open('YOUR_NAME', 'wb').write(r.content)
Afik Friedberg
  • 332
  • 2
  • 8