码迷,mamicode.com
首页 > Web开发 > 详细

urllib2

时间:2015-08-28 00:37:44      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

调用图灵机器人api

#!/usr/bin/env python
# coding=utf-8
import urllib
import urllib2

api_url=‘http://www.tuling123.com/openapi/api‘
key=‘26b1ef2041bcfac6079ce76e332c5094‘

def get_response(info):
    headers={‘User-Agent‘:‘Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)‘ }
    info1=urllib2.quote(info.encode(‘utf-8‘))
    values={‘key‘:key,‘info‘:info1}
    data=urllib.urlencode(values)
    print data
    req=urllib2.Request(api_url,data,headers)
    response=urllib2.urlopen(req)
    return response

def ex(info):
    print ‘--->>i:%s‘%info
    response1=get_response(info)
    result=response1.read()
    result=eval(result)
    result1=result[‘text‘]
    print ‘--->>rebot:%s‘ % result1

if __name__==‘__main__‘:
    while 1:
        try:
            info=raw_input(‘input some words:‘)
            ex(info)
        except KeyboardInterrupt:
            print‘我走啦‘
            break

 http://blog.sina.com.cn/s/blog_5dd2af0901012r05.html

http://www.cnblogs.com/linyawen/archive/2012/03/14/2395840.html

http://blog.csdn.net/JGood/article/details/5493824

urllib2

标签:

原文地址:http://www.cnblogs.com/yeming/p/4765156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!