简单的人机聊天
import urllib.request
import json
import string
while True:
target = r’http://api.qingyunke.com/api.php?key=free&appid=0&msg=’
Keyword = input(“想聊点儿什么:”)
if Keyword == “exit”:
print(“不聊算了,拜拜”)
break
tmp = target + Keyword
url = urllib.parse.quote(tmp, safe=string.printable)
page = urllib.request.urlopen(url)
htm1 = page.read().decode(“utf-8”)
res = json.loads(htm1) #json转为diet,json。loads 用于解码
print(res[‘content’])