我有这样的下载链接:
url = https://api.some.service:443/v2/encodedreport/13402/custom/7378/2019/05/30/MTA4Nzk2X1RlYWRzJTIwLSUyMERTVyUyMFJlcG9ydF8yMDE5MDUzMDEzMDU1OC54bHN4
当我单击链接或在浏览器中打开时,它将启动下载管理器,并且我可以单击下载以下载(xlsx)文件。我想使用python自动化下载。
我尝试使用请求:
>>> 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'}
如何以编程方式下载此文件?
答案 0 :(得分:2)
r = requests.get(url, allow_redirects=True)
open('YOUR_NAME', 'wb').write(r.content)