基于图灵api的Python机器人

一、注册图灵机器人

  1. 先注册并登录图灵机器人官网

  2. 点击创建机器人

  3. 复制机器人的key

二、搭建Python机器人

Python版本:3.6

注意替换第三行代码的apikey

import requests
import json
key = 'apikey' # 图灵机器人key,将刚刚复制的key替换apikey,保留单引号
while True:
    info = input('\n我:') # 输入对话信息
    if info == "bye" or info == "Bye" or info == "再见":  # 写入结束判断语句,满足条件跳出循环
        print('\nrobot: Bye!')
        break
    url = 'http://www.tuling123.com/openapi/api?key='+key+'&info='+info # 组成url
    re = requests.get(url) # 得到返回值
    jd = json.loads(re.text)# 将得到的json格式的信息转换为Python的字典格式
    print('\nrobot: '+jd['text'])# 输出结果
    if 'list' in jd: # 新闻信息将返回list列表
        for text in jd['list']:
            print("标题:" + text['article'])
            print("链接:" + text['detailurl'] + '\n')

三、成果展示

附:给我一点温度-梁博

posted @ 2018-03-17 12:16  梦想未必轻  阅读(1576)  评论(0编辑  收藏  举报