一、注册图灵机器人
-
先注册并登录图灵机器人官网;
-
点击创建机器人
-
复制机器人的key
二、搭建Python机器人
Python版本:3.6
注意替换第三行代码的apikey
import requests
import json
key = 'apikey' # 图灵机器人key,将刚刚复制的key替换apikey,保留单引号
while True:
info = input('
我:') # 输入对话信息
if info == "bye" or info == "Bye" or info == "再见": # 写入结束判断语句,满足条件跳出循环
print('
robot: 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('
robot: '+jd['text'])# 输出结果
if 'list' in jd: # 新闻信息将返回list列表
for text in jd['list']:
print("标题:" + text['article'])
print("链接:" + text['detailurl'] + '
')