When I try to download a file online it doesn’t work for a particular site while it works for others. Why is this happening and what should I do about it?
I write the content of the dl request in my case https://drivers.amd.com/drivers/amd-software-adrenalin-edition-22.11.1-win10-win11-nov15.exe (warning: 500+ MB file) from this page: https://www.amd.com/fr/support/graphics/amd-radeon-6000-series/amd-radeon-6900-series/amd-radeon-rx-6900-xt
JavaScript
x
15
15
1
headers = {
2
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
3
4
web = Session()
5
web.headers.update(headers)
6
7
def dl_btn(self):
8
try:
9
dl = web.get(self.link) #I use an old session that allowed me to retrieve a download link.
10
file = dl.headers['Content-Disposition'].split('=')[1]
11
file = filedialog.askdirectory() + '/' + file #Open file explorer to choose the download destination.
12
open(file, "wb").write(dl.content)
13
except:
14
popen(f'start {self.link}') #It open a page that indicate me a wrong request...
15
Advertisement
Answer
If you read the error page, it tells you exactly what the problem is: they don’t allow downloads without a referer from their website. Therefore, your headers will need to include a Referer
key/value, and I assume a proper User-Agent
as well:
JavaScript
1
15
15
1
import requests
2
from tkinter import filedialog
3
4
referer = "https://www.amd.com/fr/support/graphics/amd-radeon-6000-series/amd-radeon-6900-series/amd-radeon-rx-6900-xt"
5
file_to_dl = "https://drivers.amd.com/drivers/amd-software-adrenalin-edition-22.11.1-win10-win11-nov15.exe"
6
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
7
headers = {"User-Agent": user_agent,
8
"Referer": referer}
9
10
r = requests.get(file_to_dl, headers=headers)
11
# wait a while...
12
file = file_to_dl.split("/")[-1]
13
file_location = filedialog.askdirectory() + '/' + file
14
open(file_location, "wb").write(r.content)
15