这是脚本:
import requests
import datetime
req = requests.get('https://www.fantasylabs.com/api/sportevents/4/' + datetime.datetime.now().strftime('%m-%d-%Y') + '/team-ratings/')
data = req.json()
for teams in data:
homeTeam = teams['HomeTeamShort']
roadTeam = teams['VisitorTeamShort']
test = teams['HomeRatingsYearGoals']
print(homeTeam,roadTeam,test)
以下是错误:KeyError: 'HomeRatingsYearGoals'
如果我运行上面的脚本并且只运行print(data)
,我就会获得数据转储。
JSON数据采用以下格式:
{
"EventDetails": {
"Properties": {
"EventId": 3648610, "SportId": 4, "EventDate": "2018-01-03T00:00:00", "EventDateTime": "2018-01-03T20:00:00", "TimeOfDay": 20, "VisitorTeam": "Chicago Blackhawks", "HomeTeam": "New York Rangers", "NumberOfBetZScore": -0.707, "NumberOfBets": , "Temperature": , "WindDirection": , "WindSpeed": , "Humidity": , "HomeTimeZone": "Eastern", "VisitorTimeZone": "Central", "GameVisitorScore": , "GameHomeScore": , "Stadium": "Madison Square Garden", "ConferenceOrNon": "Non-Conference", "DivisionOrNon": "Non-Division", "PeriodDescription": , "SeasonId": 154, "SeasonType": "Regular", "TournamentName": , "RoundName": , "Periods": , "VisitorTeamShort": "CHI", "HomeTeamShort": "NYR", "DomeClosed": , "WeatherRating": , "GameIndex": 0, "EventDateId": 20180103, "NeutralSite": false, "HomeRatingsYearShotAttempts": 44.36, "HomeRatingsYearShotAttemptsPct": 55, "HomeRatingsYearGoals": 2.96, "HomeRatingsYearGoalsPct": 63, "HomeRatingsYearFantasyPointsDraftKings": 42.28, "HomeRatingsYearFantasyPointsDraftKingsPct": 70, "HomeRatingsYearFantasyPointsFanDuel": 150.55, "HomeRatingsYearFantasyPointsFanDuelPct": 69, "HomeRatingsYearFantasyPointsYahoo": 88.03, "HomeRatingsYearFantasyPointsYahooPct": 68, "HomeRatingsYearOppShotAttempts": 48.64, "HomeRatingsYearOppShotAttemptsPct": 87, "HomeRatingsYearOppGoals": 2.79, "HomeRatingsYearOppGoalsPct": 50, "HomeRatingsYearOppFantasyPointsDraftKings": 40.8, "HomeRatingsYearOppFantasyPointsDraftKingsPct": 57, "HomeRatingsYearOppFantasyPointsFanDuel": 145.14, "HomeRatingsYearOppFantasyPointsFanDuelPct": 57, "HomeRatingsYearOppFantasyPointsYahoo": 79.79, "HomeRatingsYearOppFantasyPointsYahooPct": 46, "HomeRatingsMonthShotAttempts": 41.62, "HomeRatingsMonthShotAttemptsPct": 27, "HomeRatingsMonthGoals": 2.77, "HomeRatingsMonthGoalsPct": 50, "HomeRatingsMonthFantasyPointsDraftKings": 41.75, "HomeRatingsMonthFantasyPointsDraftKingsPct": 66, "HomeRatingsMonthFantasyPointsFanDuel": 147.91, "HomeRatingsMonthFantasyPointsFanDuelPct": 63, "HomeRatingsMonthFantasyPointsYahoo": 90.46, "HomeRatingsMonthFantasyPointsYahooPct": 74, "HomeRatingsMonthOppShotAttempts": 52.0, "HomeRatingsMonthOppShotAttemptsPct": 96, "HomeRatingsMonthOppGoals": 2.38, "HomeRatingsMonthOppGoalsPct": 23, "HomeRatingsMonthOppFantasyPointsDraftKings": 39.27, "HomeRatingsMonthOppFantasyPointsDraftKingsPct": 44, "HomeRatingsMonthOppFantasyPointsFanDuel": 137.44, "HomeRatingsMonthOppFantasyPointsFanDuelPct": 38, "HomeRatingsMonthOppFantasyPointsYahoo": 72.77, "HomeRatingsMonthOppFantasyPointsYahooPct": 29, "HomeRatingsTrendShotAttempts": -6.2, "HomeRatingsTrendGoals": -6.4, "HomeRatingsTrendFantasyPointsDraftKings": -1.3, "HomeRatingsTrendFantasyPointsFanDuel": -1.8, "HomeRatingsTrendFantasyPointsYahoo": 2.8, "HomeRatingsTrendOppShotAttempts": -6.9, "HomeRatingsTrendOppGoals": 14.7, "HomeRatingsTrendOppFantasyPointsDraftKings": 3.8, "HomeRatingsTrendOppFantasyPointsFanDuel": 5.3, "HomeRatingsTrendOppFantasyPointsYahoo": 8.8, "HomeRatingsExpShotAttempts": 44.1, "HomeRatingsExpGoals": 2.9, "HomeRatingsExpFantasyPointsDraftKings": 43.0, "HomeRatingsExpFantasyPointsFanDuel": 152.7, "HomeRatingsExpFantasyPointsYahoo": 89.0, "HomeRatingsMatchupExpDraftKings": 60.0, "HomeRatingsMatchupExpFanDuel": 60.0, "HomeRatingsMatchupExpYahoo": 59.0, "VisitorRatingsYearShotAttempts": 49.05, "VisitorRatingsYearShotAttemptsPct": 89, "VisitorRatingsYearGoals": 2.98, "VisitorRatingsYearGoalsPct": 63, "VisitorRatingsYearFantasyPointsDraftKings": 43.45, "VisitorRatingsYearFantasyPointsDraftKingsPct": 79, "VisitorRatingsYearFantasyPointsFanDuel": 154.3, "VisitorRatingsYearFantasyPointsFanDuelPct": 76, "VisitorRatingsYearFantasyPointsYahoo": 89.89, "VisitorRatingsYearFantasyPointsYahooPct": 73, "VisitorRatingsYearOppShotAttempts": 45.99, "VisitorRatingsYearOppShotAttemptsPct": 69, "VisitorRatingsYearOppGoals": 2.79, "VisitorRatingsYearOppGoalsPct": 50, "VisitorRatingsYearOppFantasyPointsDraftKings": 42.44, "VisitorRatingsYearOppFantasyPointsDraftKingsPct": 72, "VisitorRatingsYearOppFantasyPointsFanDuel": 150.27, "VisitorRatingsYearOppFantasyPointsFanDuelPct": 68, "VisitorRatingsYearOppFantasyPointsYahoo": 83.32, "VisitorRatingsYearOppFantasyPointsYahooPct": 56, "VisitorRatingsMonthShotAttempts": 56.08, "VisitorRatingsMonthShotAttemptsPct": 99, "VisitorRatingsMonthGoals": 2.58, "VisitorRatingsMonthGoalsPct": 36, "VisitorRatingsMonthFantasyPointsDraftKings": 39.92, "VisitorRatingsMonthFantasyPointsDraftKingsPct": 51, "VisitorRatingsMonthFantasyPointsFanDuel": 140.26, "VisitorRatingsMonthFantasyPointsFanDuelPct": 45, "VisitorRatingsMonthFantasyPointsYahoo": 77.92, "VisitorRatingsMonthFantasyPointsYahooPct": 42, "VisitorRatingsMonthOppShotAttempts": 44.5, "VisitorRatingsMonthOppShotAttemptsPct": 56, "VisitorRatingsMonthOppGoals": 3.0, "VisitorRatingsMonthOppGoalsPct": 63, "VisitorRatingsMonthOppFantasyPointsDraftKings": 45.5, "VisitorRatingsMonthOppFantasyPointsDraftKingsPct": 89, "VisitorRatingsMonthOppFantasyPointsFanDuel": 162.0, "VisitorRatingsMonthOppFantasyPointsFanDuelPct": 87, "VisitorRatingsMonthOppFantasyPointsYahoo": 94.33, "VisitorRatingsMonthOppFantasyPointsYahooPct": 82, "VisitorRatingsTrendShotAttempts": 14.3, "VisitorRatingsTrendGoals": -13.4, "VisitorRatingsTrendFantasyPointsDraftKings": -8.1, "VisitorRatingsTrendFantasyPointsFanDuel": -9.1, "VisitorRatingsTrendFantasyPointsYahoo": -13.3, "VisitorRatingsTrendOppShotAttempts": 3.2, "VisitorRatingsTrendOppGoals": -7.5, "VisitorRatingsTrendOppFantasyPointsDraftKings": -7.2, "VisitorRatingsTrendOppFantasyPointsFanDuel": -7.8, "VisitorRatingsTrendOppFantasyPointsYahoo": -13.2, "VisitorRatingsExpShotAttempts": 51.4, "VisitorRatingsExpGoals": 2.7, "VisitorRatingsExpFantasyPointsDraftKings": 40.9, "VisitorRatingsExpFantasyPointsFanDuel": 144.3, "VisitorRatingsExpFantasyPointsYahoo": 80.1, "VisitorRatingsMatchupExpDraftKings": 64.0, "VisitorRatingsMatchupExpFanDuel": 63.0, "VisitorRatingsMatchupExpYahoo": 61.0
},
"IsLive": false
},
"EventId": 3648610, "SportId": 4, "VisitorTeam": "Chicago Blackhawks", "HomeTeam": "New York Rangers", "EventDate": "2018-01-03T00:00:00", "ReferenceKey": "", "VisitorScore": 3, "HomeScore": 2, "PeriodDescription": "3", "SeasonType": 0, "EventDateTime": "2018-01-03T20:00:00", "Spread": -1.5, "OU": 5.5, "SpreadMoney1": -225, "SpreadMoney2": 201, "OuMoney1": -131, "OuMoney2": 119, "MLMoney1": 125, "MLMoney2": -138, "HomePrimaryPlayer": "H Lundqvist", "VisitorPrimaryPlayer": "J GLASS", "VisitorPitcherThrows": "", "HomePitcherThrows": "", "StadiumId": 1679, "StadiumName": "Madison Square Garden", "StadiumStatus": 0, "StadiumType": 0, "RotoStatus": 0, "SeasonId": 154, "IsDFSEvent": 0, "SportsInsightsId": , "SportRadarId": , "RiskRating": 0, "VisitorTeamId": 2205, "HomeTeamId": 2059, "StadiumDirection": 0, "IsExcluded": false, "HomeTeamShort": "NYR", "VisitorTeamShort": "CHI", "SportEventStagingId": 0, "GameIndex": 0, "LoadWeather": true, "IsChecked": false, "HomeTeamShortVar": "NYR", "VisitorTeamShortVar": "CHI", "SpreadAbs": 1.5, "SpreadSummary": "NYR (-1.5) vs. CHI", "VisitorLineupStatus": "Projected Lineup", "HomeLineupStatus": "Projected Lineup", "VisitorLineupLockStatus": "Lineup Unlocked", "HomeLineupLockStatus": "Lineup Unlocked", "VisitorLineupConfirmed": false, "HomeLineupConfirmed": false, "VisitorLineupLocked": false, "HomeLineupLocked": false, "OddsCheckerUrl": "", "EspnId": 0, "DKContestGroups": "", "FDContestGroups": "", "FDraftContestGroups": "", "IsPPD": false, "AdjWindBearing": 0, "AdjWindBearingDisplay": 0, "EventTitle": "CHI vs NYR 8:00 PM EST", "VegasUnits": "pts", "EventSummary": "3 (3-2)", "SpreadVisitor": "", "SpreadHome": "(-1.5)", "MLVisitor": "+125", "MLHome": "-138", "SelectedTeam": , "OpposingTeam": "@NYR", "EventTime": "8:00 PM EST", "ProjHomeScore": 3.0, "ProjVisitorScore": 2.6, "IsWeatherLevel1": true, "IsWeatherLevel2": false, "IsWeatherLevel3": false, "UseWeather": false, "WeatherIcon": "", "WeatherSummary": "Roof Closed", "EventWeather": , "EventWeatherItems": []
},
{
"EventDetails": {
"Properties": {
"EventId": 3648600, "SportId": 4, "EventDate": "2018-01-03T00:00:00", "EventDateTime": "2018-01-03T19:30:00", "TimeOfDay": 19, "VisitorTeam": "Ottawa Senators", "HomeTeam": "Detroit Red Wings", "NumberOfBetZScore": 0.707, "NumberOfBets": , "Temperature": , "WindDirection": , "WindSpeed": , "Humidity": , "HomeTimeZone": "Eastern", "VisitorTimeZone": "Eastern", "GameVisitorScore": , "GameHomeScore": , "Stadium": "Little Caesars Arena", "ConferenceOrNon": "Conference", "DivisionOrNon": "Division", "PeriodDescription": , "SeasonId": 154, "SeasonType": "Regular", "TournamentName": , "RoundName": , "Periods": , "VisitorTeamShort": "OTT", "HomeTeamShort": "DET", "DomeClosed": , "WeatherRating": , "GameIndex": 0, "EventDateId": 20180103, "NeutralSite": false, "HomeRatingsYearShotAttempts": 41.7, "HomeRatingsYearShotAttemptsPct": 27, "HomeRatingsYearGoals": 2.63, "HomeRatingsYearGoalsPct": 42, "HomeRatingsYearFantasyPointsDraftKings": 37.75, "HomeRatingsYearFantasyPointsDraftKingsPct": 30, "HomeRatingsYearFantasyPointsFanDuel": 134.41, "HomeRatingsYearFantasyPointsFanDuelPct": 31, "HomeRatingsYearFantasyPointsYahoo": 71.64, "HomeRatingsYearFantasyPointsYahooPct": 26, "HomeRatingsYearOppShotAttempts": 44.19, "HomeRatingsYearOppShotAttemptsPct": 53, "HomeRatingsYearOppGoals": 3.08, "HomeRatingsYearOppGoalsPct": 71, "HomeRatingsYearOppFantasyPointsDraftKings": 41.05, "HomeRatingsYearOppFantasyPointsDraftKingsPct": 60, "HomeRatingsYearOppFantasyPointsFanDuel": 147.15, "HomeRatingsYearOppFantasyPointsFanDuelPct": 62, "HomeRatingsYearOppFantasyPointsYahoo": 87.78, "HomeRatingsYearOppFantasyPointsYahooPct": 68, "HomeRatingsMonthShotAttempts": 40.91, "HomeRatingsMonthShotAttemptsPct": 22, "HomeRatingsMonthGoals": 2.73, "HomeRatingsMonthGoalsPct": 50, "HomeRatingsMonthFantasyPointsDraftKings": 40.38, "HomeRatingsMonthFantasyPointsDraftKingsPct": 54, "HomeRatingsMonthFantasyPointsFanDuel": 142.94, "HomeRatingsMonthFantasyPointsFanDuelPct": 52, "HomeRatingsMonthFantasyPointsYahoo": 82.27, "HomeRatingsMonthFantasyPointsYahooPct": 53, "HomeRatingsMonthOppShotAttempts": 44.64, "HomeRatingsMonthOppShotAttemptsPct": 57, "HomeRatingsMonthOppGoals": 2.64, "HomeRatingsMonthOppGoalsPct": 42, "HomeRatingsMonthOppFantasyPointsDraftKings": 37.95, "HomeRatingsMonthOppFantasyPointsDraftKingsPct": 32, "HomeRatingsMonthOppFantasyPointsFanDuel": 135.82, "HomeRatingsMonthOppFantasyPointsFanDuelPct": 35, "HomeRatingsMonthOppFantasyPointsYahoo": 75.18, "HomeRatingsMonthOppFantasyPointsYahooPct": 34, "HomeRatingsTrendShotAttempts": -1.9, "HomeRatingsTrendGoals": 3.8, "HomeRatingsTrendFantasyPointsDraftKings": 7.0, "HomeRatingsTrendFantasyPointsFanDuel": 6.3, "HomeRatingsTrendFantasyPointsYahoo": 14.8, "HomeRatingsTrendOppShotAttempts": -1.0, "HomeRatingsTrendOppGoals": 14.3, "HomeRatingsTrendOppFantasyPointsDraftKings": 7.6, "HomeRatingsTrendOppFantasyPointsFanDuel": 7.7, "HomeRatingsTrendOppFantasyPointsYahoo": 14.4, "HomeRatingsExpShotAttempts": 44.0, "HomeRatingsExpGoals": 3.0, "HomeRatingsExpFantasyPointsDraftKings": 41.9, "HomeRatingsExpFantasyPointsFanDuel": 149.7, "HomeRatingsExpFantasyPointsYahoo": 87.3, "HomeRatingsMatchupExpDraftKings": 57.0, "HomeRatingsMatchupExpFanDuel": 57.0, "HomeRatingsMatchupExpYahoo": 56.0, "VisitorRatingsYearShotAttempts": 44.86, "VisitorRatingsYearShotAttemptsPct": 60, "VisitorRatingsYearGoals": 2.6, "VisitorRatingsYearGoalsPct": 36, "VisitorRatingsYearFantasyPointsDraftKings": 39.97, "VisitorRatingsYearFantasyPointsDraftKingsPct": 51, "VisitorRatingsYearFantasyPointsFanDuel": 141.63, "VisitorRatingsYearFantasyPointsFanDuelPct": 48, "VisitorRatingsYearFantasyPointsYahoo": 79.79, "VisitorRatingsYearFantasyPointsYahooPct": 46, "VisitorRatingsYearOppShotAttempts": 46.95, "VisitorRatingsYearOppShotAttemptsPct": 76, "VisitorRatingsYearOppGoals": 2.91, "VisitorRatingsYearOppGoalsPct": 63, "VisitorRatingsYearOppFantasyPointsDraftKings": 41.45, "VisitorRatingsYearOppFantasyPointsDraftKingsPct": 64, "VisitorRatingsYearOppFantasyPointsFanDuel": 147.71, "VisitorRatingsYearOppFantasyPointsFanDuelPct": 63, "VisitorRatingsYearOppFantasyPointsYahoo": 85.17, "VisitorRatingsYearOppFantasyPointsYahooPct": 61, "VisitorRatingsMonthShotAttempts": 44.54, "VisitorRatingsMonthShotAttemptsPct": 57, "VisitorRatingsMonthGoals": 1.85, "VisitorRatingsMonthGoalsPct": 5, "VisitorRatingsMonthFantasyPointsDraftKings": 33.58, "VisitorRatingsMonthFantasyPointsDraftKingsPct": 7, "VisitorRatingsMonthFantasyPointsFanDuel": 117.02, "VisitorRatingsMonthFantasyPointsFanDuelPct": 6, "VisitorRatingsMonthFantasyPointsYahoo": 56.85, "VisitorRatingsMonthFantasyPointsYahooPct": 5, "VisitorRatingsMonthOppShotAttempts": 46.46, "VisitorRatingsMonthOppShotAttemptsPct": 73, "VisitorRatingsMonthOppGoals": 3.62, "VisitorRatingsMonthOppGoalsPct": 92, "VisitorRatingsMonthOppFantasyPointsDraftKings": 48.15, "VisitorRatingsMonthOppFantasyPointsDraftKingsPct": 94, "VisitorRatingsMonthOppFantasyPointsFanDuel": 173.76, "VisitorRatingsMonthOppFantasyPointsFanDuelPct": 94, "VisitorRatingsMonthOppFantasyPointsYahoo": 110.15, "VisitorRatingsMonthOppFantasyPointsYahooPct": 95, "VisitorRatingsTrendShotAttempts": -0.7, "VisitorRatingsTrendGoals": -28.8, "VisitorRatingsTrendFantasyPointsDraftKings": -16.0, "VisitorRatingsTrendFantasyPointsFanDuel": -17.4, "VisitorRatingsTrendFantasyPointsYahoo": -28.8, "VisitorRatingsTrendOppShotAttempts": 1.0, "VisitorRatingsTrendOppGoals": -24.4, "VisitorRatingsTrendOppFantasyPointsDraftKings": -16.2, "VisitorRatingsTrendOppFantasyPointsFanDuel": -17.6, "VisitorRatingsTrendOppFantasyPointsYahoo": -29.3, "VisitorRatingsExpShotAttempts": 44.6, "VisitorRatingsExpGoals": 2.5, "VisitorRatingsExpFantasyPointsDraftKings": 38.1, "VisitorRatingsExpFantasyPointsFanDuel": 135.4, "VisitorRatingsExpFantasyPointsYahoo": 74.9, "VisitorRatingsMatchupExpDraftKings": 44.0, "VisitorRatingsMatchupExpFanDuel": 44.0, "VisitorRatingsMatchupExpYahoo": 44.0
},
"IsLive": false
},
"EventId": 3648600, "SportId": 4, "VisitorTeam": "Ottawa Senators", "HomeTeam": "Detroit Red Wings", "EventDate": "2018-01-03T00:00:00", "ReferenceKey": "", "VisitorScore": 1, "HomeScore": 2, "PeriodDescription": "FINAL", "SeasonType": 0, "EventDateTime": "2018-01-03T19:30:00", "Spread": -1.5, "OU": 5.5, "SpreadMoney1": -223, "SpreadMoney2": 199, "OuMoney1": 116, "OuMoney2": -128, "MLMoney1": 121, "MLMoney2": -134, "HomePrimaryPlayer": "J Howard", "VisitorPrimaryPlayer": "C Anderson", "VisitorPitcherThrows": "", "HomePitcherThrows": "", "StadiumId": 1609, "StadiumName": "Joe Louis Arena", "StadiumStatus": 0, "StadiumType": 0, "RotoStatus": 0, "SeasonId": 154, "IsDFSEvent": 0, "SportsInsightsId": , "SportRadarId": , "RiskRating": 0, "VisitorTeamId": 2050, "HomeTeamId": 2075, "StadiumDirection": 0, "IsExcluded": false, "HomeTeamShort": "DET", "VisitorTeamShort": "OTT", "SportEventStagingId": 0, "GameIndex": 0, "LoadWeather": true, "IsChecked": false, "HomeTeamShortVar": "DET", "VisitorTeamShortVar": "OTT", "SpreadAbs": 1.5, "SpreadSummary": "DET (-1.5) vs. OTT", "VisitorLineupStatus": "Projected Lineup", "HomeLineupStatus": "Projected Lineup", "VisitorLineupLockStatus": "Lineup Unlocked", "HomeLineupLockStatus": "Lineup Unlocked", "VisitorLineupConfirmed": false, "HomeLineupConfirmed": false, "VisitorLineupLocked": false, "HomeLineupLocked": false, "OddsCheckerUrl": "", "EspnId": 0, "DKContestGroups": "", "FDContestGroups": "", "FDraftContestGroups": "", "IsPPD": false, "AdjWindBearing": 0, "AdjWindBearingDisplay": 0, "EventTitle": "OTT vs DET 7:30 PM EST", "VegasUnits": "pts", "EventSummary": "FINAL (1-2)", "SpreadVisitor": "", "SpreadHome": "(-1.5)", "MLVisitor": "+121", "MLHome": "-134", "SelectedTeam": , "OpposingTeam": "@DET", "EventTime": "7:30 PM EST", "ProjHomeScore": 3.0, "ProjVisitorScore": 2.6, "IsWeatherLevel1": true, "IsWeatherLevel2": false, "IsWeatherLevel3": false, "UseWeather": false, "WeatherIcon": "", "WeatherSummary": "Roof Closed", "EventWeather": , "EventWeatherItems": []
}
当我只使用它们时,原始脚本将返回homeTeam
和roadTeam
,但只要我添加test = teams['HomeRatingsYearGoals']
和print(homeTeam,roadTeam,test)
,我就会收到错误。
答案 0 :(得分:2)
它是一个嵌套数据,所以你需要几步才能进入。
for teams in data:
homeTeam = teams['HomeTeamShort']
roadTeam = teams['VisitorTeamShort']
test = teams['EventDetails']['Properties']['HomeRatingsYearGoals']
print(homeTeam,roadTeam,test)