我使用Python使用Steam的API提取数据。我有一个随机生成的Steam ID列表。问题是,并非所有这些ID都指向有效帐户。当我尝试访问没有对象值的帐户时,程序会给我一个" KeyError"。 Steam API将为任何请求输出['响应级别]对象,因此当我使用该最高级别打印时,我将获得每个请求的响应。但是,如果我再进一步(例如['回复'] [' game_count']),当程序到达没有任何[' #39; game_count']值。我怎么告诉python只是跳过这些帐户?
示例输出:
帐户帐户(为了便于阅读,删除了额外的退货)
{
"response": {
"game_count": 1,
"games": [
{
"appid": 205790,
"playtime_forever": 0
}
]
}
帐号没有游戏
{
"response": {
}
}
我目前的代码:
import urllib2, random, sys, json
list = open('d:\python\SteamUserIDs.txt').read().splitlines()
SteamID = str(list)
for SteamID in list[0:10:1]:
request = urllib2.Request('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=05475AE5A8410CE01236A8A29E1DEE8E&steamid=%s&format=json' %SteamID, headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"})
response = json.load(urllib2.urlopen(request))
request2 = urllib2.Request('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=05475AE5A8410CE01236A8A29E1DEE8E&steamids=%s&format=json' %SteamID, headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"})
response2 = json.load(urllib2.urlopen(request2))
答案 0 :(得分:1)
有几种选择。
not in
。关于此问题的一个重要说明,如果您看到使用has_key()
的建议,请不要这样做。它已在Python 2.x中弃用,并在Python 3.x中删除。相反,您使用in
运算符。如果d有密钥,则返回True,否则返回False。
if 'game_count' in response:
# Do something
或者,not
版本:
if 'game_count' not in response:
# skip/pass on this record
else:
# Do something
如果key在字典中,则返回key的值,否则返回default。如果 默认情况下没有给出,默认为None,所以这个方法永远不会 提出了一个KeyError。
if response.get('game_count', None):
# Do something
else:
# skip/pass on this record
更新:您的回复没有做任何事情。这就是为什么看起来什么都没发生的原因。
您的代码还有一些需要修复的内容:
GetPlayerSummaries
,您最多可以传递100个SteamID。您不需要为每个ID一次调用一个。list
SteamID = str(list)
毫无意义,因为您正在重复使用循环中的SteamID
变量这是您的代码的略微修改版本
steam_ids = open('d:\python\SteamUserIDs.txt').read().splitlines()
steam_str = ','.join(steam_ids) # This make a comma separated string that we will use in our API calls
owned_games = {}
for s in steam_ids:
request = urllib2.Request('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=<<YOURKEY>>&steamid=%s&format=json' % s,headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"})
response = json.load(urllib2.urlopen(request))['response']
if response.get('game_count', None):
owned_games[s] = response['game_count']
else:
owned_games[s] = None
# This call will only work if you have less than 100 steam ids. If you have more, you
# will need to build steam_str differently for batches of 100
request2 = urllib2.Request('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0001/?key=<<YOURKEY>>&steamids=%s&format=json' %steam_str, headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"})
response2 = json.load(urllib2.urlopen(request2))
最后,您将得到一个owned_games
字典,如下所示(在此示例中,个人资料ID并不准确)。
{'76561197992702437': 63,
'76561197992702438': 0,
'76561197992702439': 0,
'76561197995066524': 62,
'76561197995066525': 0,
'76561197995066526': 0,
'76561197995066527': 0}
此词典向您显示每个个人资料拥有的游戏数量。然后,在response2
对象中,您可以解析response2['response']['players']
中的每个项目以进行比较。或者,您可以将players
列表重建为字典(或将其与games_owned
结合使用,这样您每次都无需遍历列表以查找相应的配置文件。但是,两个练习超出了你最初的问题范围。
现在,您需要查看response
和response2
中的数据以进行解析。
答案 1 :(得分:0)
这样的东西?
您收到密钥错误,因为密钥game_count
不存在。在做某事之前检查该密钥(例如打印)
for response in steam_id_responses:
if response.get('game_count', None):
print response