如何使用python从NBA网站获取数据

时间:2019-12-07 21:38:47

标签: python pandas web-scraping beautifulsoup

我正在尝试从NBA网站(更具体地说是从此链接https://stats.nba.com/leaders/?StatCategory=FG3M&PerMode=Totals)获取高级统计信息。但是,我似乎正在收到错误“ NoneType”对象没有属性“ tbody”。如果有人帮助我,我将不胜感激。谢谢。

我的代码

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)

2 个答案:

答案 0 :(得分:2)

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')

在线查看输出:Click Here

  

API通常很酷。

答案 1 :(得分:-2)

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)

输出:

enter image description here