gpt4 book ai didi

python - 从 API 获取 header

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:38 26 4
gpt4 key购买 nike

所以我试图从这个 API 中抓取一个表:https://api.pbpstats.com/get-wowy-combination-stats/nbaTeamId=1610612743&Season=201819&SeasonType=Playoffs&PlayerIds=203999,1627750,200794

但我无法将标题作为一个不错的列表,例如 ['Players On', 'Players Off', 'Minutes', 'NetRtg', 'OffRtg', 'DefRtg'] 对于我最终的数据框,因为 header 是它们自己的类,而不是其他类 results 的一部分。

我当前的代码如下:

import requests


url = 'https://api.pbpstats.com/get-wowy-combination-stats/nba?TeamId=1610612743&Season=2018-19&SeasonType=Playoffs&PlayerIds=203999,1627750,200794'

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
# grab table
table = response.json()['results'][0]
#grab headers
headers = response.json()['headers']

当我 print(headers) 我得到 [{'field': 'On', 'label': 'Players On'}, {'field': 'Off' , 'label': 'Players Off'}, {'field': 'Minutes', 'label': 'Minutes', 'type': 'number'}, {'field': 'NetRtg', 'label': 'NetRtg', 'type': 'decimal'}, {'field': 'OffRtg', 'label': 'OffRtg', 'type': 'decimal'}, {'field': 'DefRtg', 'label ': 'DefRtg', 'type': 'decimal'}].

是将这些放入列表的好方法,例如 ['Players On', 'Players Off', 'Minutes', 'NetRtg', 'OffRtg', 'DefRtg'] 所以我然后可以创建数据框吗?

谢谢!

最佳答案

只需从 headers 列表中提取具有特定键的所有值并制作你的字典

import requests

url = 'https://api.pbpstats.com/get-wowy-combination-stats/nba?TeamId=1610612743&Season=2018-19&SeasonType=Playoffs&PlayerIds=203999,1627750,200794'

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
#grab table
table = response.json()['results'][0]

#grab headers
headers = response.json()['headers']

#Extracting all values with every key into a dictionary
results = {}
for header in headers:
for k,v in header.items():
results.setdefault(k,[])
results[k].append(v)

#Remove duplicate elements from the list of values
results = {k:list(set(v)) for k,v in results.items()}
print(results)

输出看起来像

{
'field': ['Minutes', 'Off', 'On', 'DefRtg', 'NetRtg', 'OffRtg'],
'label': ['Minutes', 'DefRtg', 'Players On', 'NetRtg', 'OffRtg', 'Players Off'],
'type': ['decimal', 'number']
}

关于python - 从 API 获取 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com