一、注冊圖靈機器人
-
先注冊並登錄圖靈機器人官網;
-
點擊創建機器人
-
復制機器人的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')