【发布时间】:2019-11-02 21:23:53
【问题描述】:
我正在尝试从 NBA 统计数据页面获取一些统计数据。我正在遵循本教程的想法 https://towardsdatascience.com/using-python-pandas-and-plotly-to-generate-nba-shot-charts-e28f873a99cb
基本思路是将数据放入csv文件。
所以我尝试使用这段代码,从 nba 网络获取数据,尝试获取 json 文件并将其转换为 csv:
import requests
import json
import pandas as pd
from pandas import DataFrame as df
import urllib.request
shot_data_url_start="https://stats.nba.com/events/?flag=3&CFID=33&CFPARAMS=2017-18&PlayerID="
player_id="202695"
shot_data_url_end="&ContextMeasure=FGA&Season=2017-18§ion=player&sct=plot"
def shoy_chart(player_id):
full_url = shot_data_url_start + str(player_id) + shot_data_url_end
json = requests.get(full_url, headers=headers).json()
return(json)
data = json['resultSets'][0]['rowSets']
columns = json['resultSets'][0]['headers']
df = pd.DataFrame.from_records(data, columns=columns)
这是笔记本向我显示的错误:
TypeError Traceback (most recent call last)
<ipython-input-42-a3452c3a4fc8> in <module>
18
19
---> 20 data = json['resultSets'][0]['rowSets']
21 columns = json['resultSets'][0]['headers']
22
TypeError: 'module' object is not subscriptable
任何人都可以帮助我,或者知道将数据转换为 .csv 或 excel 文件的另一种方法吗?
【问题讨论】:
标签: python json csv export-to-csv export-to-excel