zoukankan      html  css  js  c++  java
  • 2019-02-15 python接口图灵机器人(简单好玩)

    import requests
    import json
    
    def Run(text):
        url = "http://openapi.tuling123.com/openapi/api/v2"#api地址
        req = {
            "reqType":0,
            "perception": {
                "inputText": {
                    "text": text
                },
                "selfInfo": {
                    "location": {
                        "city": "",
                        "province": "",
                        "street": ""  #输入自己的,可以获得天气之类的
                    }
                }
            },
            "userInfo": {
                "apiKey": "",#从图灵机器人获取
                "userId": ""#你的id
            }
        }
        req = json.dumps(req).encode('utf-8')
        post = requests.request('POST',url,data=req,headers={'content-type': 'application/json'})#发送一个post请求
        response_dic = json.loads(post.text)
        print(response_dic['results'][0]['values']['text'])#提取text
    
    
    while True:
        try:
            inputtext = input("Me(p to quit):")
            if(inputtext=='p'):
                break
            Run(inputtext)
        except:
            print("抱歉发生错误,请再次尝试")
    print("真是愉快的一次聊天啊!!")
    

    效果:在这里插入图片描述

    本来想接口到微信上的,结果微信被禁止电脑登陆了。。。

  • 相关阅读:
    08简单推导:手机尾号评分
    07简单推导:生日蜡烛
    06普通推导
    05简单推导:猴子吃桃
    简单推导
    03map用法
    List题目
    02List的使用
    01基础知识
    HDU
  • 原文地址:https://www.cnblogs.com/roccoshi/p/13027088.html
Copyright © 2011-2022 走看看