【发布时间】:2017-05-12 10:06:35
【问题描述】:
我很困惑。我从其他相关问题中尝试过的东西都没有奏效,我似乎无法通过谷歌搜索找到它。
我想用 Python 过滤一个 json api 响应,而不仅仅是对我返回的数据源进行排序——这对我来说并不是什么好事。我还想明确选择一个字段,而不是删除所有其他字段,因为有时字段比其他时候多。它必须通过请求的结构来完成,而不是事后修改任何东西。
我使用以下代码从 matchbook api 获取运动列表,但我将如何仅显示这些运动的名称,过滤掉 id 和类型?
r7 = s.get('https://api.matchbook.com/edge/rest/lookups/sports')
r8 = r7.json()
pprint.pprint (r8)
这是我回来的:
{'offset': 0,
'per-page': 20,
'sports': [{'id': 112, 'name': 'Australian Rules', 'type': 'SPORT'},
{'id': 13, 'name': 'Auto Racing', 'type': 'SPORT'},
{'id': 3, 'name': 'Baseball', 'type': 'SPORT'},
{'id': 4, 'name': 'Basketball', 'type': 'SPORT'},
{'id': 14, 'name': 'Boxing', 'type': 'SPORT'},
{'id': 110, 'name': 'Cricket', 'type': 'SPORT'},
{'id': 11, 'name': 'Current Events', 'type': 'SPORT'},
{'id': 115, 'name': 'Cycling', 'type': 'SPORT'},
{'id': 116, 'name': 'Darts', 'type': 'SPORT'},
{'id': 117, 'name': 'Gaelic Football', 'type': 'SPORT'},
{'id': 8, 'name': 'Golf', 'type': 'SPORT'},
{'id': 241798357140019,
'name': 'Greyhound Racing',
'type': 'SPORT'},
{'id': 24735152712200, 'name': 'Horse Racing', 'type': 'SPORT'},
{'id': 222109340250019,
'name': 'Horse Racing (Ante Post)',
'type': 'SPORT'},
{'id': 231138347942400,
'name': 'Horse Racing Beta',
'type': 'SPORT'},
{'id': 118, 'name': 'Hurling', 'type': 'SPORT'},
{'id': 6, 'name': 'Ice Hockey', 'type': 'SPORT'},
{'id': 126, 'name': 'MMA', 'type': 'SPORT'},
{'id': 5, 'name': 'NCAA Basketball', 'type': 'SPORT'},
{'id': 2, 'name': 'NCAA Football', 'type': 'SPORT'}],
'total': 28}
401
[Finished in 5.3s]
如何去掉“id”和“type”字段?
这是一大组json数据的摘录:
{'events': [{'allow-live-betting': True,
'category-id': [9,
297063445660036,
410444698700010,
410468520880009,
477274740480010,
486941505790009],
'id': 488243069570009,
'in-running-flag': True,
'markets': [{'allow-live-betting': True,
'back-overround': 102.04249,
'event-id': 488243069570009,
'id': 488243069830010,
'in-running-flag': True,
'lay-overround': 97.95751,
'live': True,
'market-type': 'money_line',
'name': 'Moneyline',
'runners': [{'event-id': 488243069570009,
'event-participant-id': 488243069720009,
'id': 488243069860009,
'market-id': 488243069830010,
'name': 'G Mager',
'prices': [{'available-amount': 48.42639,
'currency': 'GBP',
'decimal-odds': 1.53191,
'exchange-type': 'back-lay',
'odds': 1.53191,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 473.49579,
'currency': 'GBP',
'decimal-odds': 1.51546,
'exchange-type': 'back-lay',
'odds': 1.51546,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 96.87754,
'currency': 'GBP',
'decimal-odds': 1.40322,
'exchange-type': 'back-lay',
'odds': 1.40322,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 710.74,
'currency': 'GBP',
'decimal-odds': 1.5814,
'exchange-type': 'back-lay',
'odds': 1.5814,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 163.74192,
'currency': 'GBP',
'decimal-odds': 1.76336,
'exchange-type': 'back-lay',
'odds': 1.76336,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 151.55072,
'currency': 'GBP',
'decimal-odds': 1.9434,
'exchange-type': 'back-lay',
'odds': 1.9434,
'odds-type': 'DECIMAL',
'side': 'lay'}],
'status': 'open',
'volume': 6955.72965},
{'event-id': 488243069570009,
'event-participant-id': 488243069660010,
'id': 488243069890010,
'market-id': 488243069830010,
'name': 'A Bedene',
'prices': [{'available-amount': 413.22093,
'currency': 'GBP',
'decimal-odds': 2.72,
'exchange-type': 'back-lay',
'odds': 2.72,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 124.99383,
'currency': 'GBP',
'decimal-odds': 2.31,
'exchange-type': 'back-lay',
'odds': 2.31,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 142.97238,
'currency': 'GBP',
'decimal-odds': 2.06,
'exchange-type': 'back-lay',
'odds': 2.06,
'odds-type': 'DECIMAL',
'side': 'back'},
{'available-amount': 25.75872,
'currency': 'GBP',
'decimal-odds': 2.88,
'exchange-type': 'back-lay',
'odds': 2.88,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 244.07,
'currency': 'GBP',
'decimal-odds': 2.94,
'exchange-type': 'back-lay',
'odds': 2.94,
'odds-type': 'DECIMAL',
'side': 'lay'},
{'available-amount': 39.06352,
'currency': 'GBP',
'decimal-odds': 3.48,
'exchange-type': 'back-lay',
'odds': 3.48,
'odds-type': 'DECIMAL',
'side': 'lay'}],
'status': 'open',
'volume': 16033.12548}],
'start': '2017-05-15T11:05:00.000Z',
'status': 'open',
'type': 'binary',
'volume': 22988.85514}],
'meta-tags': [{'id': 9,
'name': 'Tennis',
'type': 'SPORT',
'url-name': 'tennis'},
{'id': 297063445660036,
'name': 'R1',
'type': 'DATE',
'url-name': 'r1'},
{'id': 410444698700010,
'name': 'Italy',
'type': 'COUNTRY',
'url-name': 'italy'},
{'id': 410468520880009,
'name': 'Live Betting',
'type': 'OTHER',
'url-name': 'live-betting'},
{'id': 477274740480010,
'name': 'May 15th 2017',
'type': 'DATE',
'url-name': 'may-15th-2017'},
{'id': 486941505790009,
'name': 'ATP Rome',
'type': 'COMPETITION',
'url-name': 'ATP-Rome'}],
'name': 'G Mager vs A Bedene',
'sport-id': 9,
'start': '2017-05-15T11:05:00.000Z',
'status': 'open',
'volume': 23098.72333},
'offset': 0,
'per-page': 20,
'total': 223}
我想在这个数据中找到 event-id 并打印 "'event-id': 488243069570009"
我设法做到了:
r11 = s.get('https://api.matchbook.com/edge/rest/events')
r12 = r11.json()
for each in r12['events'][0]['markets']:
print(each['event-id'])
但它只在完整的 json 数据集上打印第一个 event-id
【问题讨论】:
-
[ s['name'] for s in r8['sports'] ] -
那么,API 是否允许您提出这样的请求?如果没有,那你就倒霉了:你只能得到 API 提供的东西,你不能让它做它不打算做的事情。这意味着您需要发出一个请求,以最少量的“绒毛”获取您想要的数据,并自己过滤掉这些绒毛。