
Lv.1
在 从Python官网转了一圈回来,我XX了 中回复
# coding=utf-8
import json
import urllib.request
api_url = "http://openapi.tuling123.com/openapi/api/v2"
text_input = input('我:')
req = {
"perception":
{
"inputText":
{
"text": text_input
},
"selfInfo":
{
"location":
{
"city": "上海",
"province": "上海",
"street": "文汇路"
}
}
},
"userInfo":
{
"apiKey": "eaf3fa76e15喵eb5ae7ca889721997f6",
"userId": "OnlyUseAlphabet"
}
}
# print(req)
# 将字典格式的req编码为utf8
req = json.dumps(req).encode('utf8')
# print(req)
http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
response = urllib.request.urlopen(http_post)
response_str = response.read().decode('utf8')
# print(response_str)
response_dic = json.loads(response_str)
# print(response_dic)
intent_code = response_dic['intent']['code']
results_text = response_dic['results'][0]['values']['text']
print('机器人:',results_text)
2020-08-09T12:52:35 点赞:0