爬虫--request库例子
import requests
# 定义一个函数来发送HTTP请求
def send_request(method, url, **kwargs):
try:
if method == 'GET':
response = requests.get(url, **kwargs)
elif method == 'POST':
response = requests.post(url, **kwargs)
elif method == 'PUT':
response = requests.put(url, **kwargs)
elif method == 'DELETE':
response = requests.delete(url, **kwargs)
elif method == 'PATCH':
response = requests.patch(url, **kwargs)
else:
raise ValueError('Unsupported HTTP method: ' + method)
# 打印请求的URL和响应状态码
print('Requested URL:', response.url)
print('Status Code:', response.status_code)
# 返回响应内容
return response.text
except requests.exceptions.RequestException as e:
# 如果发生错误,打印错误信息
print('An error occurred:', e)
return None
# 使用函数发送请求
response_text = send_request('GET', 'https://api.example.com/data')
# 打印响应内容
print(response_text)
这个代码示例展示了如何使用Python的requests库来发送不同类型的HTTP请求,并处理可能发生的异常。这是一个简化的例子,用于教学目的。在实际应用中,你可能需要添加额外的参数和错误处理逻辑。
评论已关闭