使用Python从JSON API提取数据 [英] Extract data from JSON API using Python
本文介绍了使用Python从JSON API提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要看这部分:
如何从该URL提取数据?我只想打印出"networkdiff": 58954.60268219
.
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
这是打印网址命令导致的API显示的内容:
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
}
}
}
推荐答案
您可以使用json
模块解析出Python字典并像这样直接获取值:
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']
这篇关于使用Python从JSON API提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文