5imobi 发表于 2015-12-1 11:53:47

python实现一个图灵机器人

  这标题就是个噱头。。。其实用的别人的接口,就是这货。

  下面是代码:
  



# -*- coding: utf-8 -*-
import urllib,urllib2
import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')
API_KEY = 'bc192acc72f2768b211c193*****'
raw_TULINURL = "http://www.tuling123.com/openapi/api?key=%s&info=" % API_KEY
def result():
for i in range(1,100):
queryStr = raw_input("我:".decode('utf-8'))
TULINURL = "%s%s" % (raw_TULINURL,urllib2.quote(queryStr))
req = urllib2.Request(url=TULINURL)
result = urllib2.urlopen(req).read()
hjson=json.loads(result)
length=len(hjson.keys())
content=hjson['text']
if length==3:
return 'robots:' +content+hjson['url']
elif length==2:
return 'robots:' +content
if __name__=='__main__':
print "你好,请输入内容:".decode('utf-8')
contents=result()
print contents
  
  
  
  执行结果:
  
  下一篇中我想把它的结果放到微信公众号中。
  
页: [1]
查看完整版本: python实现一个图灵机器人