I am trying to scrape advanced stats from the NBA website, more specifically from this link https://stats.nba.com/leaders/?StatCategory=FG3M&PerMode=Totals . However, I seem to be getting the error 'NoneType' object has no attribute 'tbody'. I would appreciate it if someone helped me. Thanks.
import requests
from bs4 import BeautifulSoup
import pandas as pd
URL = 'https://stats.nba.com/leaders/?StatCategory=FG3M&PerMode=Totals'
response = requests.get(URL)
soup = BeautifulSoup(response.content, 'html.parser')
columns = ['#', 'PLAYER', 'GP', 'MIN', 'PTS', 'FGM', 'FGA', 'FG%', '3PM', '3PA',
'3P%', 'FTM', 'FTA', 'FT%', 'OREB', 'DREB', 'REB', 'AST', 'STL', 'BLK',
'TOV', 'PF', 'EFF', 'AST/TOV', 'STL/TOV']
df = pd.DataFrame(columns=columns)
table = soup.find('table').tbody
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('\n', '') for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)
df.to_csv('Stats NBA.csv', index=False)
import requests
import pandas as pd
r = requests.get(
'https://stats.nba.com/stats/leagueLeaders?LeagueID=00&PerMode=Totals&Scope=S&Season=2019-20&SeasonType=Regular+Season&StatCategory=FG3M').json()
df = pd.DataFrame(r['resultSet']['rowSet'], columns=r['resultSet']['headers'])
df.to_csv('output.csv', index=False)
print('done')
View Output Online: Click Here
API
is cool usually.
import requests
import pandas as pd
r = requests.get(
'https://stats.nba.com/stats/leagueLeaders?LeagueID=00&PerMode=Totals&Scope=S&Season=2020-21&SeasonType=Regular+Season&StatCategory=FG3M').json()
df = pd.DataFrame(r['resultSet']['rowSet'],
columns=r['resultSet']['headers'])
df.to_csv('NBA stats.csv', index=False)
print(df)
output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.