random-cat
random-cat copied to clipboard
HTTP Error 403: Forbidden
Traceback (most recent call last):
File "/Users/icebunny08/Documents/a_test.py", line 3, in <module>
cat.getCat(directory="~/Downloads", filename="cat", format='png')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/cat/__init__.py", line 41, in getCat
download(downloadlink, savefile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 247, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
The only file in this is init.py. You can replace the existing init.py with the attached init.txt (just change the extension) for this to work init.txt
The API changed so you can no longer specify filetype. You can get more images, specify breeds, etc. if you get the API key and make a few more code changes https://developers.thecatapi.com/view-account/ylX4blBYT9FaoVd6OhvR?report=bOoHBz-8t
Can be fixed by passing a (fake) header to requests.get
Following this thread: https://stackoverflow.com/questions/38489386/how-to-fix-403-forbidden-errors-when-calling-apis-using-python-requests
I added
headers = {'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'}
and then changed the function to call
r = requests.get(url, headers=headers)