我在Matchbook投注API中使用以下代码。

r17 = s.get('https://matchbook.com/bpapi/rest/events/?sport-ids=15&?after=1486157894&?before=14862442917&')
data1 = r17.json()

for event in data1['events']:
    print(event['name'])
    print(event['id'])
    print(event['sport-id'])
    print(event['start'])
    print(event['meta-tags'])


这给出了以下json输出

Bayern Munich vs Schalke
368063
15
2017-02-04T14:35:00.000Z
[{'id': 1, 'url-name': 'sport', 'name': 'Sport', 'type': 'UNKNOWN'}, {'id': 402, 'url-name': 'live-betting', 'name': 'Live Betting', 'type': 'COMPETITION'}, {'id': 4, 'url-name': 'soccer', 'name': 'Soccer', 'type': 'SPORT'}, {'id': 56, 'url-name': 'germany', 'name': 'Germany', 'type': 'COUNTRY'}, {'id': 57, 'url-name': 'bundesliga', 'name': 'Bundesliga', 'type': 'COMPETITION'}, {'id': 4105, 'url-name': 'february-4th-2017', 'name': 'February 4th 2017', 'type': 'DATE'}]


元标记包含在[]括号之间。如何按这些元标记过滤?

最佳答案

import pprint

r17 = requests.get('https://matchbook.com/bpapi/rest/events/?sport-ids=15&?after=1486157894&?before=14862442917&')
data = r17.json()

for event in data['events']:
    print(event['name'])
    pprint.pprint(event['meta-tags'], indent=4)
    print('sorted:')
    # change k['id'] to k['name'] if you need to sort dict's by name
    pprint.pprint(sorted(event['meta-tags'], key=lambda k: k['id']), indent=4)

关于python - 如何使用Python Requests过滤json响应中的元标记?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42041923/

10-08 21:42