Getting this error:
File "/usr/local/lib/python3.11/urllib/request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
headers:
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Accept-Encoding": "none",
"Accept-Language": "en-US,en;q=0.8",
"Connection": "keep-alive",
}
Despite using User-Agent in the request's headers, I'm getting this error: HTTP Error 403: Forbidden
My code:
import urllib
req = urllib.request.Request(url=url, headers=headers)
urllib.request.urlopen(req).read().decode("utf-8") ##error is coming from this line.
NOTE: This code is working with most of the URL.
HTTP returns
403 Forbiddenwhen you try to access to a path you have no authority over. It shouldn't be the problem of the code. Check if you are allowed to access to the url and add required information throughheadersorcookieto prove that you have the allowance to access to the path.