September 2018
Intermediate to advanced
426 pages
10h 46m
English
Errors in requests are handled differently from other modules. The following example generates a 404 error indicating that it cannot find the requested resource:
>>> response = requests.get('http://www.google.com/pagenotexists')>>> response.status_code404
In this case, the requests module returns a 404 error. To see the exception generated internally, we can use the raise_for_status () method:
>>> response.raise_for_status()requests.exceptions.HTTPError: 404 Client Error
In the event of making a request to a host that does not exist, and once the timeout has been produced, we get a ConnectionError exception:
>>> r = requests.get('http://url_not_exists')requests.exceptions.ConnectionError: HTTPConnectionPool(... ...Read now
Unlock full access