I'm using Python to extract data using Steam's API. I have a randomly generated list of Steam ID's. The problem is, not all of these ID's point to valid accounts. When I try to access the account for which there is no object value, the program gives me a "KeyError". The Steam API will output a ['response'] level object for any requests, so when i print using that topmost level, I will get a response for every request. However, if I go one more level in (e.g. ['response']['game_count']), the program will fail when it gets to an account which does not have any ['game_count'] value. How do I tell python to just skip these accounts?
Example output:
account with game (Removed extra returns for readability)
{
"response": {
"game_count": 1,
"games": [
{
"appid": 205790,
"playtime_forever": 0
}
]
}
account without game
{
"response": {
}
}
My current code:
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))