其实大家如果想着做一个页面与机器人聊天是可以做到的,这里呢楼主只是给大家做一个框架,在shell里和机器人聊天,页面望各位开发者自行开发
创建一个机器人
楼主这里以青云客智能聊天机器人为例,可以点击下面卡片进入官网
青云客智能聊天机器人APIhttp://api.qingyunke.com/不需要注册,直接翻到最末尾就可以看到对话框,其他平台经过楼主测试,需要注册,还有一部分需要付费,请诸位开发者自行选择
库的准备
我们本次的程序,需要爬虫requests库需要诸位开发者自行安装
程序编写
- 只访问一次
import requests url = 'http://api.qingyunke.com/api.php' text = input('Please enter a question:') params = { 'appid':'0', 'key':'free', 'msg':text } response = requests.get(url = url,params = params) response = response.json() response = response['content'] print(response)
这样唯一的缺陷就是只能运行一次,但是不能达成对话的要求,所以说我们应该加一个while的无限循环
-
while True无限循环对话
import requests url = 'http://api.qingyunke.com/api.php' while True: text = input('Please enter a question:') params = { 'appid':'0', 'key':'free', 'msg':text } response = requests.get(url = url,params = params) response = response.json() response = response['content'] print(response)
这样就可以达成我们和机器人聊天的条件
-
我提起一个话题,机器人和自己无限的聊下去(看个乐子)
import requests url = 'http://api.qingyunke.com/api.php' text = input('Please enter first question:') while True: params = { 'appid':'0', 'key':'free', 'msg':text } response = requests.get(url = url,params = params) response = response.json() response = response['content'] text = response print(response)
这样就达成了我们给机器人一个话题,机器人自己和自己无限的聊下去
怎么样?各位开发者们学废了吗?快去完成你们的开发项目吧!