Skip to content
Home » urllib2.HTTPError: HTTP Error 403: Forbidden

urllib2.HTTPError: HTTP Error 403: Forbidden

To solve urllib2.HTTPError: HTTP Error 403: Forbidden error follow below methods. Given below the error log of the issue.

ERROR LOG

File "C:\Python27\lib\urllib2.py", line 527, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 403: Forbidden

How to solve urllib2.HTTPError: HTTP Error 403: Forbidden ?

Refer to the given methods to solve the issue.

Method 1:

Maybe some of the headers are missing. Add the following headers and try again.

all_headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       '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'}
req = urllib2.Request(site_url, headers=all_headers)

Hope the above solution works.

Also read :