Extract data from JSON API using Python [duplicate]

旧时模样 提交于 2019-11-28 21:57:28

You can use the json module to parse out a Python dictionary and get right to the value like so:

import json
result = json.loads(url)  # result is now a dict
print '"networkdiff":', result['getpoolstatus']['data']['networkdiff']

To do this multiple times (to answer your question in the comments section):

import json
import urllib

urls = {'joe': 'url1', 'jack': 'url2', 'jane': 'url3'}
for who in urls.keys():
    url = urllib.urlopen(urls[who])
    result = json.loads(url)  # result is now a dict
    print 'For %s: "networkdiff":' % who, result['getpoolstatus']['data']['networkdiff']

convert the response to json and then read it

from urllib import urlopen
import simplejson as json

url = urlopen('http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41').read()
url = json.loads(url)

print url.get('getpoolstatus').get('data').get('networkdiff')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!