urllib request returning 403 error. How to rectify?

Viewed 60
url_csv = "example_url"
local_path_csv = "./example.csv"
_ = urllib.request.urlretrieve(url_csv, local_path_csv)

Example of output:

HTTPError: HTTP Error 403: This request is not authorized to perform this operation.

1 Answers

The HTTP 403 is an HTTP status code meaning access to the requested resource is forbidden.

So the requested resource is just blocking your request. You should specify the real URL for further investigation.

But the first step you should do is to specify headers in your request like this

import urllib.request

opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36')]
urllib.request.install_opener(opener)

url_csv = "example_url"
local_path_csv = "./example.csv"
_ = urllib.request.urlretrieve(url_csv, local_path_csv)
Related