亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 中的字典從 Hockey API 中抓取數據

使用 Python 中的字典從 Hockey API 中抓取數據

RISEBY 2023-05-09 15:06:07
我開始從事一個有趣的項目,以更好地練習我的數據抓取技能,我從 NHL API 抓取數據并嘗試記錄射門和進球的所有位置坐標(此 API 將向您顯示任何 NHL 比賽并具有坐標以及在所述游戲中發生的每個事件的玩家信息)。但是,我在通過數據編制索引時遇到問題,而且我真的不確定如何處理它。下面是我的代碼...import requests as rqimport csvGAME_ID = "2017021121" #Game ID indicates which game I want to look at...first 4 digits is the year, second two the point in season, (01 Pre, 02 Reg, 03 Playoffs, 04 All Star)#URL to access the coordinates of every event in given game...comes in nested dictionary formurl = f"https://statsapi.web.nhl.com/api/v1/game/{GAME_ID}/feed/live"game = rq.get(url)#turn the file into a readable onecontents = game.text#split text into list so we can fool around with itcontents_list = list(csv.reader(contents.splitlines()))def main():    file = open( f'coordinates.{GAME_ID}.txt', 'a')我現在要做的是使用 for 循環遍歷數據集并檢查“事件類型”以及它們是否等于“射門”或“目標”,以及它們是否要添加它們的值x, y 坐標到打印到新文件中的字典。我已經嘗試通過自己建立索引,但我不太擅長數據抓取,所以我沒有走得太遠。作為參考,這里是數據集的樣子(或至少是它的一個片段)。} ],        "result" : {          "event" : "Penalty",          "eventCode" : "COL162",          "eventTypeId" : "PENALTY",          "description" : "Blake Coleman Tripping against Erik Johnson",          "secondaryType" : "Tripping",          "penaltySeverity" : "Minor",          "penaltyMinutes" : 2        },        "about" : {          "eventIdx" : 30,          "eventId" : 162,          "period" : 1,          "periodType" : "REGULAR",          "ordinalNum" : "1st",          "periodTime" : "04:47",          "periodTimeRemaining" : "15:13",          "dateTime" : "2019-03-17T19:15:33Z",          "goals" : {            "away" : 0,            "home" : 0          }        },        "coordinates" : {          "x" : -58.0,          "y" : -37.0        },對我來說,它看起來像是一堆嵌套的字典,但我又不太確定。任何幫助將不勝感激??!謝謝你??!
查看完整描述

2 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

它看起來像一個大列表,包含字典列表。您可以使用for-each-loop.

for entry in list:

然后,您可以單獨查看每個條目并檢查它們是否eventTypeId等于shotgoal

if entry["result"]["eventTypeId"] == "SHOT":

在這種情況下,您可以像這樣提取 X 和 Y 坐標的值:

x = entry["coordinates"]["x"]
y = entry["coordinates"]["y"]

之后,您可以使用這些坐標來做任何您想做的事情。


查看完整回答
反對 回復 2023-05-09
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

您可以遍歷列表,通過將所需數據放入字典中以使用 pandas 構建表。


import requests as rq

import pandas as pd


GAME_ID = "2017021121" #Game ID indicates which game I want to look at...first 4 digits is the year, second two the point in season, (01 Pre, 02 Reg, 03 Playoffs, 04 All Star)


#URL to access the coordinates of every event in given game...comes in nested dictionary form

url = f"https://statsapi.web.nhl.com/api/v1/game/{GAME_ID}/feed/live"

game = rq.get(url).json()

game = game['liveData']['plays']['allPlays']


rows = []

for each in game:

    if each['result']['event'] in ['Shot','Goal']:

        print(each['result']['event'])

        row = {

                'result':each['result']['event'],

                'x':each['coordinates']['x'],

                'y':each['coordinates']['y']}

        rows.append(row)

        

df = pd.DataFrame(rows)

輸出:


print(df)

   result     x     y

0    Shot  76.0  -8.0

1    Shot -41.0 -24.0

2    Shot  69.0  -1.0

3    Shot  37.0  30.0

4    Shot -60.0   1.0

5    Goal  43.0 -20.0

6    Shot -85.0   6.0

7    Shot -83.0  11.0

8    Shot -85.0  -5.0

9    Shot  81.0  -1.0

10   Shot -84.0  -7.0

11   Shot -81.0  -3.0

12   Shot -57.0  12.0

13   Shot  74.0 -14.0

14   Goal  64.0   0.0

15   Shot  68.0   2.0

16   Shot  73.0   5.0

17   Shot  63.0  -2.0

18   Shot -26.0 -16.0

19   Shot -84.0  -2.0

20   Shot -74.0  36.0

21   Shot  83.0   6.0

22   Shot -88.0   7.0

23   Shot -39.0  30.0

24   Shot -69.0 -22.0

25   Shot  65.0  32.0

26   Shot  80.0  -3.0

27   Shot -70.0  -6.0

28   Shot -87.0  17.0

29   Shot -29.0 -20.0

..    ...   ...   ...

47   Shot  81.0   1.0

48   Shot -82.0   0.0

49   Shot  38.0 -28.0

50   Shot  66.0  -9.0

51   Shot  49.0   5.0

52   Shot -48.0 -12.0

53   Shot  39.0  20.0

54   Goal  75.0  -9.0

55   Shot -84.0  -7.0

56   Shot  88.0 -28.0

57   Shot  41.0 -20.0

58   Shot -41.0  12.0

59   Shot  40.0   7.0

60   Shot  72.0  -4.0

61   Goal  86.0   7.0

62   Shot -77.0  -8.0

63   Shot  34.0 -32.0

64   Shot -79.0 -10.0

65   Shot  61.0   6.0

66   Shot -43.0  17.0

67   Shot -41.0  -5.0

68   Shot -35.0  19.0

69   Shot -45.0 -11.0

70   Shot -77.0  -8.0

71   Shot  80.0  -1.0

72   Shot -87.0   7.0

73   Shot  84.0  24.0

74   Shot  76.0   7.0

75   Goal  83.0 -18.0

76   Shot -81.0  -5.0


[77 rows x 3 columns]



查看完整回答
反對 回復 2023-05-09
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號