Python类型中的HTTP请求

2024-04-20 13:50:58 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么此代码返回异常:

raise TypeError(f'Object of type {o.class.name} 
TypeError: Object of type set is not JSON serializable

我的代码:

import requests

json_data  = {"Челябинская обл, г Челябинск, ул Ворошилова, д 55"}
resp = requests.post('https://dadata.ru/api/v2/suggest/address', json=json_data)
resp.json()

print(resp.json()[0]['region_fias_id'])

Tags: of代码namejsondataobjectistype
1条回答
网友
1楼 · 发布于 2024-04-20 13:50:58

正如上面所说,您的json_data变量是一个集合

根据docs of the API you are using,看起来您需要一个带有“查询”键的dict:

json_data  = {"query": "Челябинская обл, г Челябинск, ул Ворошилова, д 55"}

尽管也要注意,您需要在该帖子的标题中包含API密钥:

headers = {"Authorization": "Token {}".format(API_KEY)}
resp = requests.post('https://dadata.ru/api/v2/suggest/address', json=json_data, headers=headers)

相关问题 更多 >