Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';str';对象不支持Python中的项分配,Web抓取_Python_Loops_Web Scraping - Fatal编程技术网

';str';对象不支持Python中的项分配,Web抓取

';str';对象不支持Python中的项分配,Web抓取,python,loops,web-scraping,Python,Loops,Web Scraping,我正在尝试重组我已经存在/正在工作的代码。 在第二个for循环内进行更改。 为什么从以下位置更改代码时会出现错误: import numpy as np import pandas as pd import requests import json from sklearn import preprocessing from sklearn.preprocessing import OneHotEncoder games_played = [] stats_for_games = [] for

我正在尝试重组我已经存在/正在工作的代码。 在第二个for循环内进行更改。 为什么从以下位置更改代码时会出现错误:

import numpy as np
import pandas as pd
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder

games_played = []
stats_for_games = []
for game_id in range(2017020001, 2017020010, 1):
    url = 'https://statsapi.web.nhl.com/api/v1/game/{}/boxscore'.format(game_id)
    r_2017 = requests.get(url)
    game_data_2017 = r_2017.json()

    for homeaway in ['home','away']:

        game_dict_2017 = game_data_2017.get('teams').get(homeaway).get('teamStats').get('teamSkaterStats')
        game_dict_2017['team'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
        game_dict_2017['homeaway'] = homeaway
        game_dict_2017['game_id'] = game_id
        games_played.append(game_dict_2017)

print(games_played)
为此:

import numpy as np
import pandas as pd 
import requests
import json
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder

games_played = []
stats_for_games = []
    for game_id in range(2017020001, 2017020010, 1):
    url = 'https://statsapi.web.nhl.com/api/v1/game/{}/boxscore'.format(game_id)
    r_2017 = requests.get(url)
    game_data_2017 = r_2017.json()

    for homeaway in ['home','away']:

        game_dict_2017 = game_data_2017.get('teams').get(homeaway).get('team').get('name')
        game_dict_2017['homeaway'] = homeaway
        game_dict_2017['game_id'] = game_id
        games_played.append(game_dict_2017)

print(games_played)
运行时,错误TypeError:“str”对象不支持项分配

出现在

 game_dict_2017['homeaway'] = homeaway
JSON数据示例

{
copyright: "NHL and the NHL Shield are registered trademarks of the 
National Hockey League. NHL and NHL team marks are the property of the 
NHL and its teams. © NHL 2019. All Rights Reserved.",
teams: {
     away: {
         team: { 
                id: 9,
                name: "Ottawa Senators",
                link: "/api/v1/teams/9"
 },
teamStats: {
    teamSkaterStats: {
                   goals: 0,
                   pim: 0,
                   shots: 0,

 }
 }
这就是问题所在


你定义了2017年的游戏,我想它是一个字符串。然后试着用它作为字典,我想你把2017年的《游戏规则》和2017年的《游戏数据》搞砸了,好吧,我终于明白问题所在了

以前的代码是:

# In this line you created a dictionary
game_dict_2017 = game_data_2017.get('teams').get(homeaway).get('teamStats').get('teamSkaterStats')

# Here, since you have dictionary you rewrote values or created new one
game_dict_2017['team'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
game_dict_2017['homeaway'] = homeaway
game_dict_2017['game_id'] = game_id
games_played.append(game_dict_2017)
目前的代码是:

# Here you are getting a string
# You previous code was 
# game_dict_2017['team'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
game_dict_2017 = game_data_2017.get('teams').get(homeaway).get('team').get('name')

# Here you are trying to work with a string as with dictionary
game_dict_2017['homeaway'] = homeaway
game_dict_2017['game_id'] = game_id
games_played.append(game_dict_2017)

为了解决此问题,假设您不需要
game\u dict\u 2017=game\u data\u 2017.get('teams').get(homeaway).get('teamStats').get('teamSkaterStats')
行代码如下:

game_dict_2017 = dict()
game_dict_2017['team'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
game_dict_2017['homeaway'] = homeaway
game_dict_2017['game_id'] = game_id
您也可以这样做:

game_dict_2017 = {
    'team': game_data_2017.get('teams').get(homeaway).get('team').get('name'),
    'homeaway': homeaway,
    'game_id': game_id
}
此行返回一个字符串,即“Winnipeg Jets”。 一个快速修复方法是首先初始化game_dict_2017,然后将dict键设置为团队名称

示例:

    game_dict_2017 = dict()
    game_dict_2017['teamname'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
    game_dict_2017['homeaway'] = homeaway
    game_dict_2017['game_id'] = 2017020001
    games_played.append(game_dict_2017)

game_dict_2017=game_data_2017.get('teams').get(homeaway.get('team').get('name')
在这个linw中,您获取字典的一个值,并尝试获取另一个值和另一个值,等等。除非您有嵌套字典,否则它将无法工作。JSON数据是嵌套字典,这就是我使用该方法的原因。以前工作过您可以添加您正在尝试使用的数据样本吗?我认为你只是错误地将<代码> GAMEJORDIOS2017改写成一个字符串,在代码> GAMEJORDATION2017=GAMEYDATA2017。获取(“团队”)。获取(“团队”)。获取(“名称”)< /代码>,然后尝试与 GAMMYDATA201201< <代码>一起工作。
    game_dict_2017 = game_data_2017.get('teams').get(homeaway).get('team').get('name')
    game_dict_2017 = dict()
    game_dict_2017['teamname'] = game_data_2017.get('teams').get(homeaway).get('team').get('name')
    game_dict_2017['homeaway'] = homeaway
    game_dict_2017['game_id'] = 2017020001
    games_played.append(game_dict_2017)