Я обнаружил, что для загрузки файла, обрабатываемого некоторыми javascript, вы должны отправить запрос на публикацию с теми же данными, как если бы вы делали его вручную в своем браузере. Таким образом, вам потребуются такие же файлы cookie и параметры запроса, как если бы вы вручную нажимали на ссылку. Вы можете использовать инспектор веб-браузера (F12) и воспроизвести запрос, чтобы найти все эти параметры.
Вот пример (имейте в виду, что это c специально для моей проблемы):
# -*- coding: utf-8 -*-
import requests
from requests.auth import HTTPBasicAuth
//First, authenticate and get the session cookies in the authentication page
url1 = 'http://YourSiteAuthenticationPage.com'
session = requests.Session()
response = session.get(url1)
cookies = session.cookies.get_dict()
//In my case, I also had to add some parts of the cookies to the URL
url1 += "blabla/download.do;sessionid=" + cookies.get("SESSIONID")
response = session.get(url1, auth = HTTPBasicAuth('username', 'password'), cookies=cookies)
//Then fill headers and datas needed (found with browser inspector on the page)
url = "UrlOfTheFile/file.xls"
headers = {
"Host":"yoursite.com",
"Proxy-Connection": "keep-alive",
"Content-Length": "98",
"Cache-Control": "max-age=0",
"Upgrade-Insecure-Requests": "1",
"Origin": "http://idc.yolo.com",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/12.0 (Windows NT 8.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Referer": "depend on you",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"Cookie": "stillDependOnYou; secondCookie=07"
}
//These are also found with inspector in the network part
form_data = {
"subAction": "onDownloadDoDis",
"filename": "nameOfYourFile.xls"
}
//Then send the post request to the file's URL and download it
rep = requests.post(url, headers=headers, data=form_data, cookies=cookies)
//Write the content to a fresh new file on your computer
file = open("test.xls",'wb')
file.write(rep.content)
file.close()