Instead of the try/except block and checking `r.status_code`, just use `r.raise_for_status()`, which raises an exception on any of the common HTTP errors that requests can throw.  That way you don't have all these indent levels.