2016-12-07 65 views
0

我想獲得NHL賽季每場比賽的統計數據和比賽信息。我正在與Stata合作。我發現包nhlscrapi,並已編寫的代碼來獲取所有數據和特定季節的統計:nhlscrapi - 下載數據錯誤

# Import statements 
# Notice how I import the whole modules and not the functions explicitly as given in the online example (good practice) 
from nhlscrapi.games import game, cumstats 
from nhlscrapi import constants 
import csv 


# Define season being considered: 
season = 2012 

# Get all stats they have defined 
# Googled "get all methods of a class python" and found this: 
# http://stackoverflow.com/questions/34439/finding-what-methods-an-object-has 
# Also, needed to excclude some methods (ABCMeta, ...) after I checked what they do 
# (I did that with: "help(cumstats.METHODNAME)") and saw that they did not contain stats 
methods = [method for method in dir(cumstats) if callable(getattr(cumstats, method)) and 
               method != 'ABCMeta' and 
               method != 'AccumulateStats' and 
               method != 'ShotEventTallyBase' and 
               method != 'abstractmethod' and 
               method != 'TeamIncrementor' and 
               method != 'EF' and 
               method != 'St'] 

# Set up dictionary with all stats 
cum_stats = {method: getattr(cumstats, method)() for method in methods} 

print('All the stats:', cum_stats.keys()) 

# Now, look up how many games were in the regular season of the year 2012 
maxgames = constants.GAME_CT_DICT[season] 

# If one is interested in all the home coaches (as an example), one would first set up an empty list, 
# and gradually fill it: 
thingswewant_keys = ['home_coach', 'away_coach', 'home', 'away', 'attendance', 'Score', 'Fenwick'] 
thingswewant_values = {key: [] for key in thingswewant_keys if not key in cum_stats.keys()} 
thingswewant_values.update({key+'_home': [] for key in cum_stats.keys()}) 
thingswewant_values.update({key+'_away': [] for key in cum_stats.keys()}) 

# Now, loop over all games in this season 
for i in range(**12**): 
    # Set up object which queries database 
    # If one enters the following command in ipython: "help(game.Game)", one sees also alternative ways to set up 
    # query other than the one given in the example 
    ggames = game.Game(game.GameKey(season, game.GameType.Regular, i+1), cum_stats=cum_stats) 

    # This object 'ggames' now contains all the information of 1 specific game. 
    # To concatenate all the home coaches for example, one would do it like this 

    for key in thingswewant_keys: 
     if not key in cum_stats.keys(): 
      # First case: Information is attribute of ggames (e.g. home_coach) 
      if not key in ['home', 'away', 'attendance']: 
       thingswewant_values[key] += [getattr(ggames, key)] 

      # Second case: Information is key of ggames.matchup (e.g. home) 
      if key in ['home', 'away', 'attendance']: 
       thingswewant_values[key] += [ggames.matchup[key]] 

    # Third case: Information is a cum_stat 
    # Figure out home_team and away team 
    hometeam = ggames.matchup['home'] 
    awayteam = ggames.matchup['away'] 

    for key in cum_stats.keys(): 
     thingswewant_values[key+'_home'] += [ggames.cum_stats[key].total[hometeam]] 
     thingswewant_values[key+'_away'] += [ggames.cum_stats[key].total[awayteam]] 

# Make one single table out of all the columns 
results = [tuple([key for key in thingswewant_values.keys()])] 
results += zip(*[thingswewant_values[key] for key in thingswewant_values.keys()]) 

# Write to csv 
with open('brrr.csv', 'wb') as f: 
    writer = csv.writer(f) 
    writer.writerows(results) 

現在的問題是,在每一個季節,有一定的比賽結束後,代碼將停止並吐出以下錯誤:

Traceback (most recent call last): 
    File "C:/Users/Dennis/Downloads/AllStatsExcell.py", line 67, in <module> 
    thingswewant_values[key+'_home'] += [ggames.cum_stats[key].total[hometeam]] 
    File "C:\Python27\lib\site-packages\nhlscrapi\games\game.py", line 211, in cum_stats 
    return self.play_by_play.compute_stats() 
    File "C:\Python27\lib\site-packages\nhlscrapi\games\playbyplay.py", line 95, in compute_stats 
    for play in self._rep_reader.parse_plays_stream(): 
    File "C:\Python27\lib\site-packages\nhlscrapi\scrapr\rtss.py", line 56, in parse_plays_stream 
    p_obj = parser.build_play(p) 
    File "C:\Python27\lib\site-packages\nhlscrapi\scrapr\rtss.py", line 130, in build_play 
    p['vis_on_ice'] = self.__skaters(skater_tab[0][0]) if len(skater_tab) else { } 
    File "C:\Python27\lib\site-packages\nhlscrapi\scrapr\rtss.py", line 159, in __skaters 
    if pl[0].text.isdigit(): 
AttributeError: 'NoneType' object has no attribute 'isdigit' 

在2012賽季,這個遊戲發生後12所以我只是在賽季的第12場運行2012

​​

在例如,,它崩潰。但是如果我再次運行這條線,我會得到結果。

我不知道如何解決這個問題。

如果我只是可以得到所有遊戲的csv文件,即使有一些缺失值,我也會很高興。

回答

1

首先,您需要自己做一些調試。該錯誤明確指出:

File "C:/Users/Dennis/Downloads/AllStatsExcell.py", line 67, in thingswewant_values[key+'_home'] += [ggames.cum_stats[key].total[hometeam]]

這意味着您的程序的第67行有錯誤。在底部它表明你是什麼錯誤是:

AttributeError: 'NoneType' object has no attribute 'isdigit'

這意味着,你正在試圖將屬性isdigit上的對象,它是NoneType的價值。正如你可能猜測的那樣,NoneType對象沒有任何內容。

這是有問題的線路,與塊前面沿:

for key in cum_stats.keys(): 
    thingswewant_values[key+'_home'] += [ggames.cum_stats[key].total[hometeam]] 

你想要做什麼可能是以下幾點:

for key in cum_stats.keys(): 
    try: 
     thingswewant_values[key+'_home'] += [ggames.cum_stats[key].total[hometeam]] 
    except Exception as e: 
     print(e) 
     print("key={}".format(key) 
     print("hometeam={}".format(hometeam) 
     print("ggames.cumstats={}".format(s[key].total[hometeam]) 

這是一個基本錯誤捕獲塊。第一個打印行應該告訴你這個例外。下面的告訴你關於你在違規行中使用的各種事情的狀態。你的工作是弄清楚哪個是NoneType(它可能不是我提供的那個),然後,找出爲什麼它是NoneType。基本上是:看看你有的數據,並試圖在該塊中操縱。它缺少一些東西。

+0

謝謝納撒尼爾。我會嘗試這個 – Evidenz

+0

如果有幫助,不要忘記用你發現的任何附加信息來上傳和更新你的問題(假設你沒有解決問題)。當你鼓勵人們更可能幫助你。 –