#-*- version: Python3.0 -* #-*- coding: UTF-8 -* import urllib import urllib.request import sys import ssl import json # client_id 为官网获取的AK, client_secret 为官网获取的SK host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=eiBsX7AmncXIANL7yCGojkTA&client_secret=OnqEu2UEfSd6aPA7evwHYGg5EFRE0Tia' request = urllib.request.Request(host) request.add_header('Content-Type', 'application/json; charset=UTF-8') response = urllib.request.urlopen(request) resp_taken = response.read() #if (resp_taken): # print(resp_taken) text = json.loads(resp_taken) #print(text["access_token"]) headers = {'Content-Type':'application/json'} access_token = text["access_token"] url = 'https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=' + access_token post_data = "{\"log_id\":\"UNITTEST_10000\",\"version\":\"2.0\",\"service_id\":\"S20837\",\"session_id\":\"\",\"request\":{\"query\":\"你几岁了\",\"user_id\":\"88888\"},\"dialog_state\":{\"contexts\":{\"SYS_REMEMBERED_SKILLS\":[\"1057\"]}}}" request = urllib.request.Request(url,data=post_data.encode('utf-8'),headers=headers) response = urllib.request.urlopen(request) content = response.read().decode("utf-8") #if content: # print(content) text1 = json.loads(content) print(text1['result']['response_list'][0]['action_list'][0]['say'])