Evan
Evan

Reputation: 383

How can I load NBA data from a webpage into a dataframe?

How do I load this beautiful table into a pandas dataframe? Note that this is from an older season so it is not dynamic data if that affects the methodology at all.

Thanks!

Upvotes: 0

Views: 66

Answers (1)

chitown88
chitown88

Reputation: 28630

You really do need to provide code on what you have tried. But since this takes about a minute to answer, I'll provide the solution:

Go into Inspect (ctrl-shft-I) and look within the Network -> XHR tab. Search through those requests and see if you can find what you need in "Preview". Once you find it, go to "Headers" and "Payload" to get the relevant info to feed into your requests.

import pandas as pd
import requests

url = 'https://stats.nba.com/stats/leaguedashlineups'
headers = {
    'Referer': 'https://www.nba.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'}
payload = {
'Conference': '',
'DateFrom': '',
'DateTo': '',
'Division': '',
'GameID': '',
'GameSegment': '',
'GroupQuantity': '5',
'LastNGames': '0',
'LeagueID': '00',
'Location': '',
'MeasureType': 'Base',
'Month': '0',
'OpponentTeamID': '0',
'Outcome': '',
'PORound': '0',
'PaceAdjust': 'N',
'PerMode': 'Per36',
'Period': '0',
'PlusMinus': 'N',
'Rank': 'N',
'Season': '2011-12',
'SeasonSegment': '',
'SeasonType': 'Regular Season',
'ShotClockRange': '',
'TeamID': '0',
'VsConference': '',
'VsDivision':  ''   }


jsonData = requests.get(url, headers=headers, params=payload).json()
cols = jsonData['resultSets'][0]['headers']

df = pd.DataFrame(jsonData['resultSets'][0]['rowSet'],
                  columns=cols) 

Output:

enter image description here

Upvotes: 1

Related Questions