首先,json的格式:
www.tuling123.com/openapi/api?key=$api_key&info=$req_info
import json import requests import urllib import urllib2 KEY = '***********************' # change to your API KEY url = 'http://www.tuling123.com/openapi/api' req_info = u'讲个笑话'.encode('utf-8') query = {'key': KEY, 'info': req_info} headers = {'Content-type': 'text/html', 'charset': 'utf-8'} # 方法一、用requests模块已get方式获取内容 r = requests.get(url, params=query, headers=headers) res = r.text print json.loads(res).get('text').replace('<br>', 'n') # 方法二、用urllib和urllib2库获取内容 data = urllib.urlencode(query) req = urllib2.Request(url, data) f = urllib2.urlopen(req).read() print json.loads(f).get('text').replace('<br>', 'n')
原载:http://www.myexception.cn/perl-python/1885404.html