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'])

相关文章:

  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-05-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2022-02-02
  • 2021-10-03
  • 2021-11-15
  • 2022-12-23
相关资源
相似解决方案