I am trying to download a files from API via Python requests. I cannot emulate a browser despite using the headers. I tried using requests.get(link, headers=headers) and it did not work. I also don't understand why session.get(link).headers lacks the 'user-agent'. I always get "The requested URL was rejected." even though the link works in a browser.
import requests # requests 2.32.3
# File from the returned resources api.I am also interested in downloading XLSX files.
link = "https://open.data.gov.sa/odp-public/61ebb13c-d3b3-4cee-8edd-0f0d71923d9a/d79e9bfc-ae6b-4504-84cb-e6010d88aebd/v1/Tabuk University graduates intermediate diploma.csv"
#Obtained from https://httpbin.org/headers
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "sv-SE,sv;q=0.9",
"Host": "httpbin.org",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15",
"X-Amzn-Trace-Id": "Root=1-68836664-4337d8c5727c410265c779f0"
}
session = requests.Session()
session.headers.update(headers)
print(session.get(link).text)
https://open.data.gov.sa/data/api/datasets?version=-1&dataset=d79e9bfc-ae6b-4504-84cb-e6010d88aebd