You can check the status code and decide what to do in each case. For example:
def data(ticker):
url = "https://eodhistoricaldata.com/api/fundamentals/{ticker}.LSE?api_token".format(ticker=ticker)
with requests.get(url, verify=True) as response:
if response.status_code == 401:
print("Not authorized")
elif response.status_code == 404:
print("Not found")
else:
data = response.json()
# your logic
You can explore the response
object and use it to define more fine grained behaviour (for example: use the reason
attribute or print the content of the response).
CLICK HERE to find out more related problems solutions.