我正在尝试从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)
答案 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)
输出: