python获取post返回json
时间: 2023-09-09 15:14:18 浏览: 119
可以使用Python中的requests库来发送POST请求,并获取返回的JSON数据。
具体实现步骤如下:
1. 导入requests库
```python
import requests
```
2. 构造POST请求,设置请求头和请求体
```python
url = 'http://example.com/api'
headers = {'Content-Type': 'application/json'}
data = '{"key": "value"}'
response = requests.post(url, headers=headers, data=data)
```
3. 解析返回的JSON数据
```python
json_data = response.json()
```
完整代码示例:
```python
import requests
url = 'http://example.com/api'
headers = {'Content-Type': 'application/json'}
data = '{"key": "value"}'
response = requests.post(url, headers=headers, data=data)
json_data = response.json()
print(json_data)
```
其中,`url`是POST请求的目标URL,`headers`是请求头,`data`是请求体,`response.json()`方法将返回的JSON数据解析为Python字典。
阅读全文
相关推荐















