未完成・python爬虫類第8関プロジェクトはチャットできるロボットを作成しました.


要求:実現機能:チャットできるロボットを作成する
import requests
import json


userid = str(1)
# 1            32     
apikey = str(‘A')
#    A,         apikey ~


#   post  
def robot(content):
    #   api
    api = r'http://openapi.tuling123.com/openapi/api/v2'
    #   post     
    data = {
        "perception": {
            "inputText": {
                "text": content
                         }
                      },
        "userInfo": {
                    "apiKey": apikey,
                    "userId": userid,
                    }
    }
    #    json  
    jsondata = json.dumps(data)
    #   post  
    response = requests.post(api, data = jsondata)
    #     json    
    robot_res = json.loads(response.content)
    #       
    print(robot_res["results"][0]['values']['text'])


for x in range(10):
    content = input("talk:")
    #       
    robot(content)
    if x == 10:
        break
        #          ,      ,