最后编辑于2018-05
Python3-urllib3-API UNIT示例代码
- AccessToken获取可以参考:http://ai.baidu.com/forum/topic/show/497663(Python3-urllib3示例)
- Python安装什么的。大家百度经验即可
-----------------------------------------------------下面开始代码----------------------------------------------------
- Python3-API示例代码(UNIT)
'''
Created on 2018-02-07
UNIT接口-Python3 -API示例代码
@author: 小帅丶
'''
import urllib3
import json
access_token='自己的token'
http=urllib3.PoolManager()
url='https://aip.baidubce.com/rpc/2.0/solution/v1/unit_utterance?access_token='+access_token
params={'scene_id':自己的场景id,'query':'北京天气如何','session_id':''}
#接口要求json格式。所以需要用json这个库,具体用法请自行百度。
encoded_data = json.dumps(params).encode('utf-8')
print(encoded_data)
request=http.request('POST',
url,
body=encoded_data,
headers={'Content-Type':'application/json'})
#对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
result = str(request.data,'utf-8')
print(result)
- 返回的识别结果内容
{
"log_id": 151796866200516,
"result": {
"action_list": [
{
"action_id": "0",
"action_type": {
"act_type": "",
"act_target": "",
"act_target_detail": "",
"act_type_detail": ""
},
"arg_list": [ ],
"confidence": 99,
"exe_status": [ ],
"main_exe": "",
"say": "前几天天气很好,这两天雾霾有点严重."
},
{
"action_id": "0",
"action_type": {
"act_type": "",
"act_target": "",
"act_target_detail": "",
"act_type_detail": ""
},
"arg_list": [ ],
"confidence": 98,
"exe_status": [ ],
"main_exe": "",
"say": "看天气预报"
},
{
"action_id": "0",
"action_type": {
"act_type": "",
"act_target": "",
"act_target_detail": "",
"act_type_detail": ""
},
"arg_list": [ ],
"confidence": 97,
"exe_status": [ ],
"main_exe": "",
"say": "今天还算晴朗前几天有点风有点冷"
}
],
"bot_session": "",
"session_id": "a1ffd08e50f847cb8a8ef567acce5a811"
}
-----------------------------------------------------代码结束-----------------------------------------------------
确实发现Python简单。而且写的代码也很少。代码仅供参考。
小帅丶的QQ:783021975
请登录后评论
TOP
切换版块
v1就是1.0的
这是1.0的api吗?
不错的文档,对于我这个不懂程序的人来说是很好的资料。
Python语言的简洁性是毋庸置疑~