如何从API中获取NHL团队名称和赔率?

2024-06-06 08:00:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望得到以下信息:

1)NHL团队名称

2)下注赔率

这是网站:https://www.bovada.lv/services/sports/event/v2/events/A/description/hockey/nhl。在

以下是我目前为止的代码:

import requests

source = requests.get("https://www.bovada.lv/services/sports/event/v2/events/A/description/hockey/nhl").json()   

data = source[0]    

for game in data['events'][2].items():
    print(game)

上面的代码只打印一个游戏的数据。我希望所有的比赛。有没有一种简单的方法来遍历所有这些?我也不知道如何解析出我要找的信息。在

  • 球队名称

我注意到团队名称位于标题为“竞争对手”的部分:

^{pr2}$

在上面的例子中,我要查找的信息分别是费城飞人队纽约游骑兵队。在

  • 胜算

(注:几率会发生变化,因此您看到的数字可能会有所不同)

我还注意到概率分布位于标题为“显示组”的部分:

"displayGroups":[{"id":"100-128","description":"Game Lines","defaultType":true,"alternateType":false,"markets":[{"id":"57349611","description":"Total","key":"2W-OU","marketTypeId":"120743","status":"O","singleOnly":false,"notes":"","period":{"id":"1191","description":"Match","abbreviation":"M","live":false,"main":true},"outcomes":[{"id":"294606239","description":"Over","status":"O","type":"O","price":{"id":"1836755921","handicap":"6.5","american":"EVEN","decimal":"2.00","fractional":"1/1","malay":"1.00","indonesian":"1.00","hongkong":"1.00"}},{"id":"294606240","description":"Under","status":"O","type":"U","price":{"id":"1836755922","handicap":"6.5","american":"-125","decimal":"1.800","fractional":"4/5","malay":"0.80","indonesian":"-1.25","hongkong":"0.80"}}]},{"id":"57349614","description":"Moneyline","key":"2W-12","marketTypeId":"372","status":"O","singleOnly":false,"notes":"","period":{"id":"1191","description":"Match","abbreviation":"M","live":false,"main":true},"outcomes":[{"id":"294606235","description":"New York Rangers","status":"O","type":"A","competitorId":"3596982-376","price":{"id":"1836569815","american":"+125","decimal":"2.250","fractional":"5/4","malay":"-0.80","indonesian":"1.25","hongkong":"1.25"}},{"id":"294606236","description":"Philadelphia Flyers","status":"O","type":"H","competitorId":"3596982-510","price":{"id":"1836569814","american":"-155","decimal":"1.645161","fractional":"20/31","malay":"0.65","indonesian":"-1.55","hongkong":"0.65"}}]},{"id":"57349618","description":"Puck Line","key":"2W-HCAP","marketTypeId":"120744","status":"O","singleOnly":false,"notes":"","period":{"id":"1191","description":"Match","abbreviation":"M","live":false,"main":true},"outcomes":[{"id":"294606248","description":"New York Rangers","status":"O","type":"A","competitorId":"3596982-376","price":{"id":"1836755920","handicap":"1.5","american":"-210","decimal":"1.47619","fractional":"10/21","malay":"0.48","indonesian":"-2.10","hongkong":"0.48"}},{"id":"294606249","description":"Philadelphia Flyers","status":"O","type":"H","competitorId":"3596982-510","price":{"id":"1836569799","handicap":"-1.5","american":"+175"

在上面的例子中,我要查找的信息是1.5-210(对于纽约游骑兵队),以及-1.5+175(对于费城飞人队)。在

以下是我想要的一个游戏的数据示例:

                            Spread       Win         Total
New York Rangers        +1.5 (-230)     +130     O 6.0 (-105)
Philadelphia Flyers     -1.5 (+190)     -150     O 6.0 (-115)

如果可能的话,我希望所有的游戏都有相同的信息。在

我是新的刮片和没有以前的经验编码。任何帮助将不胜感激。提前感谢您的时间和努力!在

  • 更新:

以下是URL:https://www.bovada.lv/sports/hockey

我正在查找下注行信息:

enter image description here


Tags: 信息idfalsetruetypestatusdescriptionprice
2条回答

这是一个JSON格式的数据,所以使用python json module

import json
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> ['foo', {'bar': ['baz', None, 1.0, 2]}]

您可以使用调试器来观察数据结构,loads(str)函数将返回。在您的情况下,它将是dict。然后在列表和字典中导航。在

要获取团队名称对,请尝试以下操作:

import requests

source = requests.get("https://www.bovada.lv/services/sports/event/v2/events/A/description/hockey/nhl").json()   

data = source[0] 
for game in data:
    for team in data['events']:
        try:
            team_1 = team['competitors'][0]['name']
            team_2 = team['competitors'][1]['name']
            try: odd_1_1 = team['displayGroups'][0]['markets'][0]['outcomes'][0]['price']['handicap'] 
            except KeyError: odd_1_1 = None
            try: odd_1_2 = team['displayGroups'][0]['markets'][0]['outcomes'][0]['price']['american'] 
            except KeyError: odd_1_2 = None
            try: odd_2_1 = team['displayGroups'][0]['markets'][0]['outcomes'][1]['price']['handicap'] 
            except KeyError: odd_2_1 = None
            try: odd_2_2 = team['displayGroups'][0]['markets'][0]['outcomes'][1]['price']['american'] 
            except KeyError: odd_2_2 = None
            print("{0} ({2}, {3}) vs {1} ({4}, {5})".format(team_1,team_2, odd_1_1, odd_1_2, odd_2_1, odd_2_2))
        except IndexError:
            pass

输出:

^{pr2}$

注意,在某些条目中competitors键没有值,所以我使用try/except来处理这种情况。。。在

相关问题 更多 >