我完成了这一部分:
如何从该网址中提取数据?我只想打印出"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
这是打印url命令显示的API:
{
"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
}
}
}
答案 0 :(得分:17)
您可以使用json
模块解析Python字典并直接获取值,如下所示:
import json
result = json.loads(url) # result is now a dict
print '"networkdiff":', result['getpoolstatus']['data']['networkdiff']
多次这样做(在评论部分回答你的问题):
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']
答案 1 :(得分:2)
将响应转换为json,然后将其读取
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')