官网给出的调用示例是Python2的代码,Py3直接使用会报错,经过自己的摸索,使用Py3改写成功了,分享给大家(*^▽^*)
官网Python2源代码链接:https://ai.baidu.com/docs#/UNIT-v2-API/top
# -*- coding: utf-8 -*-
import urllib.request as ul_re
import json
url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=' + 【你的access_token】
post_data = {
"bot_session": "",
"log_id": "7758521",
"request": {
"bernard_level": 0,
"client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",
"query": "【你的输入语句】",
"query_info": {
"asr_candidates": [],
"source": "KEYBOARD",
"type": "TEXT"
},
"updates": "",
"user_id": "88888"
},
"bot_id": 【你的BOTID】,
"version": "2.0"
}
encoded_data = json.dumps(post_data).encode('utf-8')
headers = {'Content-Type': 'application/json'}
request = ul_re.Request(url, data=encoded_data, headers=headers)
response = ul_re.urlopen(request)
content = response.read()
result = str(content,'utf-8')
print(result)
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
棒~~~~
感觉也可以尝试使用urllib3呀.