Write me a program in python that given a game\_id (for example 0022200688) can get the…
Private research annotation
Sign in to add a private research label or note.
- User
Write me a program in python that given a game_id (for example 0022200688) can get the boxscore for that game from the NBA website
- Assistant
To get the boxscore for a particular NBA game from the NBA website using Python, you can use the requests library to make an HTTP request to the NBA's API endpoint for boxscores, and then parse the JSON response to extract the relevant data. Here's an example implementation:
pythonimport requests # Define the API endpoint and the game_id to retrieve api_endpoint = "https://data.nba.com/data/v2015/json/mobile_teams/nba/2022/scores/gamedetail/0022200688_gamedetail.json" game_id = "0022200688" # Make an HTTP GET request to the API endpoint response = requests.get(api_endpoint) # Parse the JSON response into a Python dictionary data = response.json() # Extract the boxscore data from the dictionary boxscore = data["g"]["pd"][0]["bs"]["@data"] # Print the boxscore data print(boxscore) - User
Now instead of box score, get the play by play. Put it all inside a function that receives the game_id as an argument and returns the playbyplay json object. It should also check if the "game_id.json" already exists and if not, it should dump the json onto a file
- Assistant
Sure, here's a modified version of the code that retrieves the play-by-play data instead of the boxscore, and wraps everything in a function that takes a game_id argument:
python