【基础】使用requests库进行API调用
发布时间: 2024-06-27 15:33:59 阅读量: 8 订阅数: 23 ![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_vip.0fdee7e1.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/col_vip.0fdee7e1.png)
![【基础】使用requests库进行API调用](https://img-blog.csdnimg.cn/c049d8b0e88645a29adeb55f6b66d5ce.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5pyo5a2Q6KaB5LiK6L-b5ZGA,size_20,color_FFFFFF,t_70,g_se,x_16)
# 1. API调用的基本概念和原理**
API(应用程序编程接口)是一种软件接口,允许不同应用程序之间进行交互。它为应用程序提供了一个标准化的方式来访问和使用其他应用程序的功能和数据。
API调用是应用程序向API发送请求并接收响应的过程。请求通常包含有关请求的操作、要访问的数据以及其他相关信息。响应包含请求结果或错误消息。
API调用遵循客户端-服务器模型,其中客户端应用程序向服务器应用程序发送请求,服务器应用程序处理请求并返回响应。API调用的基本原理包括:
* **请求-响应模型:**客户端发送请求,服务器返回响应。
* **端点:**API中可访问的特定资源或操作。
* **方法:**用于指定请求类型的HTTP方法(如GET、POST、PUT、DELETE)。
* **参数:**请求中传递的附加信息。
* **响应代码:**服务器响应的HTTP状态代码,指示请求的状态。
# 2. requests库的安装和使用
### 2.1 requests库的安装和配置
requests库是Python中用于发送HTTP请求和处理HTTP响应的第三方库。要安装requests库,可以使用pip命令:
```
pip install requests
```
安装完成后,可以在Python脚本中导入requests库:
```python
import requests
```
### 2.2 发送HTTP请求的基本语法
requests库提供了多种方法来发送HTTP请求,最常用的方法是get()和post()。get()方法用于发送GET请求,post()方法用于发送POST请求。
发送HTTP请求的基本语法如下:
```python
response = requests.get(url, params=None, headers=None, cookies=None, timeout=None)
```
其中:
* url:请求的URL地址
* params:GET请求的参数,以字典形式传递
* headers:请求头信息,以字典形式传递
* cookies:请求携带的cookies,以字典形式传递
* timeout:请求超时时间,单位为秒
例如,发送一个GET请求到百度首页:
```python
response = requests.get("https://www.baidu.com")
```
### 2.3 处理HTTP响应
发送HTTP请求后,会得到一个HTTP响应对象。响应对象包含了服务器返回的HTTP状态码、响应头信息和响应内容。
要获取HTTP状态码,可以使用response.status_code属性:
```python
status_code = response.status_code
```
要获取响应头信息,可以使用response.headers属性:
```python
headers = response.headers
```
要获取响应内容,可以使用response.text属性:
```python
content = response.text
```
例如,打印百度首页响应的状态码和响应内容:
```python
print(response.status_code)
print(response.text)
```
输出:
```
200
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="baidu-site-verification" content="7e1877b703f529f253d0c26c29f56f09" />
<title>百度一下,你就知道</title>
<link href="https://www.baidu.com/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="https://www.baidu.com/static/search/css/result.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/rich_media.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/common.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/result_v2.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v2.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v3.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v4.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v5.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v6.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v7.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v8.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v9.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v10.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v11.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v12.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v13.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v14.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v15.css" rel="stylesheet" type="text/css" />
<link href="https://www.baidu.com/static/search/css/new_result_v16.css" rel="stylesheet" type="text/css" />
<link h
```
0
0
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![chm](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)