pythonrequests 404
时间: 2023-10-14 15:15:45 浏览: 147
python requests
A 404 error in Python requests means that the requested URL was not found on the server. This could be due to incorrect URL, incorrect server configuration, or the resource being removed or moved to a different location.
To handle a 404 error in Python requests, you can check the status code of the response object. If the status code is 404, you can handle the error accordingly, for example by printing an error message or raising an exception.
Here is an example:
```
import requests
url = 'https://example.com/nonexistent-page'
response = requests.get(url)
if response.status_code == 404:
print('Error: Page not found!')
# or raise an exception
else:
# handle the response
```
In this example, if the requested page does not exist, the program will print an error message. Otherwise, it will proceed with handling the response.
阅读全文