How to write a Python script to download file from the link that starts downloading automatically when clicked?

I have a link: https://www.cmegroup.com/CmeWS/exp/voiProductDetailsViewExport.ctl?media=xls&tradeDate=20220114&reportType=F&productId=425

If you copy this url and paste it to the browser, .xls file will start downloading

How to write a Python script to download the .xls file programmatically? So I can run .py file to download .xls file

If it was a link pointing to the file directly e.g. "http://google.com/favicon.ico" then it would be straightforward, something like:

import requests
url = 'http://google.com/favicon.ico'
r = requests.get(url)
open('google.ico', 'wb').write(r.content)

but since my link is not just the link to the file, this solution doesn't work

Please help to write the Python script to download the .xls file from the following link https://www.cmegroup.com/CmeWS/exp/voiProductDetailsViewExport.ctl?media=xls&tradeDate=20220114&reportType=F&productId=425


Solution 1:

you can get the current day date from the DateTime module and strip that out with strftime and change your URL like this:

from datetime import date
import requests

today = date.today()
todayStr= date.today().strftime('%Y%m%d')
url = "https://www.cmegroup.com/CmeWS/exp/voiProductDetailsViewExport.ctl?media=xls&tradeDate="+todayStr+"&reportType=F&productId=425"

r = requests.get(url)
open(todayStr+'.xls', 'wb').write(r.content)