Python爬虫中的网页请求与响应错误示例
在Python的网络爬虫中,常见的网页请求与响应错误主要包括以下几个示例:
HTTP请求错误:
URLError
:如果URL本身有误(如无效链接、编码问题等),会抛出此异常。
```python
import requests
try:
response = requests.get('http://example.com/invalid') # 错误的URL
print(response.text)
except requests.RequestException as e:
print(f"Error occurred: {e}")
```
HTTP状态码错误:
HTTPStatus
:Python标准库中的httpstatus
模块提供了一系列HTTP状态码,例如404表示找不到资源。
```python
import httpstatus
try:
response = requests.get('http://example.com/invalid') # 错误的URL
if response.status_code == httpstatus.HTTPStatus.NOT_FOUND: # 检查是否为404状态码
print("Resource not found.")
else:
print(f"Response status code: {response.status_code}")
except requests.RequestException as e:
print(f"Error occurred: {e}")
```
网络连接错误:
ConnectionError
:Python的内置异常,表示在尝试连接时发生了错误。try:
response = requests.get('http://example.com/invalid') # 错误的URL
print(response.text)
except (requests.RequestException, ConnectionError)) as e:
print(f"Error occurred: {e}")
以上就是Python爬虫中常见的网页请求与响应错误示例。
还没有评论,来说两句吧...