I am using Python 3.7.0. When I run the following code, I get the error listed below:
import requests
url = f'https://statsapi.mlb.com/api/v1/schedule?sportId=1&hydrate=probablePitcher&date={2023-06-28}'
response = requests.get(url)
games = response.json()['dates'][0]['games']
for game in games:
prob_home = game['teams']['home']['probablePitcher'].get('fullName')
Error:
Traceback (most recent call last):
File "fakepath\test.py", line 39, in <module>
prob_home = game['teams']['home']['probablePitcher'].get('fullName')
KeyError: 'probablePitcher'
How is this possible? The documentation says that get can never return a KeyError. I have also tried with "None" specified and it still gives the same KeyError. I can get through most of the games listed but only once there is no specified probablePitcher do I get a KeyError.
The KeyError occurs when the key you are trying to access is not present in the dictionary. In your case, the error is happening because the 'probablePitcher' key is missing in the 'home' team dictionary for that particular game.
Basically you are trying to get fullName
from probablePitcher
but probablePitcher
does not exist in the dict.
While it's true that the get() method can help you avoid KeyError by providing a default value when the key is not found, it seems like you are not using get() in this specific line of code. To handle this situation, you can modify your code to check if the key exists before accessing its value. Here's an updated version of your code that incorporates this change:
import requests
url = f'https://statsapi.mlb.com/api/v1/schedule?sportId=1&hydrate=probablePitcher&date={2023-06-28}'
response = requests.get(url)
games = response.json()['dates'][0]['games']
for game in games:
probable_pitcher = game['teams']['home'].get('probablePitcher')
if probable_pitcher is not None:
prob_home = probable_pitcher['fullName']
print(prob_home)
else:
print("No probable pitcher specified for the home team.")
In the updated code, probable_pitcher is assigned the value of 'probablePitcher' key from the 'home' team dictionary. If the key is not found (i.e., probable_pitcher is None), it indicates that no probable pitcher is specified for the home team in that game. You can handle this case accordingly.
I've searched the json in your url and as you can see
"gamePk": 717586,
"link": "/api/v1.1/game/717586/feed/live",
"gameType": "R",
"season": "2023",
"gameDate": "2023-06-28T23:07:00Z",
"officialDate": "2023-06-28",
"status": {
"abstractGameState": "Preview",
"codedGameState": "S",
"detailedState": "Scheduled",
"statusCode": "S",
"startTimeTBD": false,
"abstractGameCode": "P"
},
"teams": {
"away": {
"leagueRecord": {
"wins": 45,
"losses": 34,
"pct": ".570"
},
"team": {
"id": 137,
"name": "San Francisco Giants",
"link": "/api/v1/teams/137"
},
"probablePitcher": {
"id": 657277,
"fullName": "Logan Webb",
"link": "/api/v1/people/657277"
},
"splitSquad": false,
"seriesNumber": 26
},
"home": {
"leagueRecord": {
"wins": 43,
"losses": 37,
"pct": ".538"
},
"team": {
"id": 141,
"name": "Toronto Blue Jays",
"link": "/api/v1/teams/141"
},
"splitSquad": false,
"seriesNumber": 26
}
},
There is no probable pitcher in this case.