如何从API中抓取NHL球队的名称和赔率?

时间:2018-11-22 20:03:07

标签: python json api parsing web-scraping

我希望获得以下信息:

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)

上面的代码仅显示一个游戏的数据。我一直希望所有的比赛。有没有一种简单的方法可以遍历所有这些对象?我也不确定如何解析我想要的信息。

  • 团队名称

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

competitors":[{"id":"3596982-510","name":"Philadelphia Flyers","home":true},{"id":"3596982-376","name":"New York Rangers","home":false}]

在上面的示例中,我要查找的信息分别是费城传单纽约游骑兵

  • 赔率

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

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

"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)

我希望所有游戏都提供相同的信息。

我是新手,也没有编码经验。任何帮助将不胜感激。预先感谢您的时间和精力!

  • 更新:

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

我正在寻找下注信息:

enter image description here

2 个答案:

答案 0 :(得分:2)

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

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

输出:

Philadelphia Flyers (6.0, -115) vs New York Rangers (6.0, -105)
Washington Capitals (1.5, -150) vs Detroit Red Wings (-1.5, +125)
Anaheim Ducks (5.5, -110) vs Edmonton Oilers (5.5, -110)
...

请注意,在某些条目中,competitors键没有任何值,因此我使用了try / except处理这种情况...

答案 1 :(得分:1)

这是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。然后浏览列表和字典。