Extract data from JSON API using Python [duplicate]
Asked Answered
C

2

12

I go through this part:

How do I extract the data from that URL? I only want to print out the "networkdiff": 58954.60268219.

from urllib import urlopen

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

print url

This is what the API display as a result from print url command:

{
    "getpoolstatus": {
        "version": "1.0.0",
        "runtime": 16.618967056274,
        "data": {
            "pool_name": "21 Coin Pool @ Luckyminers.com",
            "hashrate": 485426748,
            "efficiency": 98.1,
            "workers": 14,
            "currentnetworkblock": 12025,
            "nextnetworkblock": 12026,
            "lastblock": 12023,
            "networkdiff": 58954.60268219,
            "esttime": 521.61956775542,
            "estshares": 241478052.58625,
            "timesincelast": 427,
            "nethashrate": 485426748
        }
    }
}
Centuple answered 10/2, 2014 at 6:14 Comment(1)
If there's an answer you like, please help the community and mark it as accepted by clicking the check mark.Saleratus
S
21

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']
Saleratus answered 10/2, 2014 at 6:27 Comment(2)
I like your approach with the dictionary approach.Centuple
Now I have 3 URLs that I want to get the 'networkdiff' data to display. How do I create a dictionary with 3 different names. Joe, Jack, Jane and at the same time display the three 'networkdiff' next to those 3 names.Centuple
S
2

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')
Spikes answered 10/2, 2014 at 6:25 Comment(6)
Why use simplejson instead of json?Gusher
what do I do next after I use your code? how does it display the networkdiff number for me to see?Centuple
@Centuple print prints it.Gusher
simplejson has backwards compatibility and is updated quite often you may use try: import simplejson as json except ImportError: import json for fallback safety.Spikes
u may install simplejson by pip install simplejson or else if on python 2.6+ can simply use jsonSpikes
I don't know how to install simplejson on windows 7 so i'm switching to Ubuntu now, it comes with it.Centuple

© 2022 - 2024 — McMap. All rights reserved.