python - nhlscrapi-下载数据错误

标签 python error-handling download

我正在尝试获取NHL赛季每场比赛的统计信息和比赛信息。我正在与Stata合作。我找到了 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场比赛之后。因此,我只参加了2012赛季的第12场比赛。
ggames1=game.Game(game.GameKey(2012, game.GameType.Regular, 12),cum_stats=cum_stats
ggames1.cum_stats['ShootOut'].total 

例如,在ShootOut中,它崩溃了。但是,如果我再次运行此行,则会得到结果。

我不知道该如何解决。

如果我能得到所有游戏的csv文件,即使缺少一些值,我也会很高兴。

最佳答案

首先,您需要自己进行一些调试。该错误明确指出:

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块:
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])

这是一个基本的错误捕获块。第一行将告诉您异常(exception)情况。以下内容可通知您有关在违规行中正在使用的各种事物的状态。您的工作是弄清楚哪个是NoneType(它可能不是我提供的内容之一),然后,确定它为什么是NoneType。本质上:查看您拥有的数据并尝试在该块中进行操作。它缺少一些东西。

关于python - nhlscrapi-下载数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41010876/

相关文章:

python - 如何更改嵌套列表中第二个元素的数据类型

error-handling - 禁用 Jetty 中的默认堆栈跟踪错误响应

android - 从 Android 网站下载

java - 如何使用 Selenium 读取pdf文件

bash - 使用 wget 从站点下载类似文件

python - Tensorflow将tf.CsvDataset.map()转换为Bert输入格式

python - 检测帧特定区域中的对象

python - Beautifulsoup:当我尝试使用 Beautifulsoup4 访问 soup.head.next_sibling 值时换行

javascript - 如何彻底处理提取中的预期错误?

scala - 如何在scala的catch表达式中打印失败的索引?