我有这样的下载链接:
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'PKx03x04x14x00x08x08x08x00xbdhxbeNx00x00x00x00x00x00x00x00x00x00x00x00x0bx00x00x00_rels/.relsxadx92MKx031x10x86xffJx98{7xdb*"xd2xb4x17x11zx13xa9?`Lf?xd8M&$S]xffxbdAx0fxdaxb2x85n=x0e3xefxf3>x87Yo'?xaawJxb9xe7``Yxd5xa0(Xv}hrxbcxeex9fx16xf7xa0xb2`p8r x03x81axbbYxbfxd0x8...
# 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'}
如何以编程方式下载此文件?
r = requests.get(url, allow_redirects=True)
open('YOUR_NAME', 'wb').write(r.content)