python请求接口,抓取返回的数据
代码如下:
# 引入包
import urllib2
import json
import sys
url = 'http://alisleepy.top' # 接口地址
responseResult = urllib2.urlopen(url, None, timeout=2000)
code = responseResult.getcode()
print '接口状态码:' + str(code)
if code != 200:
sys.exit('request failed,please check interface is correct!')
# 检查数据,是否符合预期
data = json.loads(responseResult.read()) # 反序列化json数据
print 'code码:' + str(data['code])
print 'msg信息:' + str(data['msg'])