python 实践
Python 练习读取url Api 并解析
#coding=utf-8
import urllib
import json
#转中文
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
def main():
customer = 'qq'
url = "http://192.168.100.223:8888/rcms-api/customer/" + customer + "/channels"
response = urllib.urlopen(url).read()
#获取json格式
json_obj = json.loads(response)
channels = {}
#获取json 格式类型 和长度
print '%s %d' %(type(json_obj), len(json_obj))
#解析json, 转换成map字典
for data in json_obj:
channels] = data['name']
#map字典长度
print len(channels)
#循环输出
for channel in channels.keys():
print '%s %s' %(channel, channels)
if __name__ == '__main__':
main()
页:
[1]