1

Im trying to make a Python script that gets a list of the items in someone's Steam inventory. I have this code currently:

#!/usr/bin/python
import urllib2
import json
def getInventory(steamid):
        data = urllib2.urlopen('http://steamcommunity.com/profiles/'+steamid+'/inventory/json/730/2')
        json_data = json.loads(data)
        print 'Success: %s' %  json_data['success']

        for v in json_data['rgDescriptions']:
                print 'Item: ' + v['name']

        print('Done!')
        return

getInventory('76561197988445370');

But it won't output anything other than 'Success: True' and 'Done!'. Can someone help me get this to work?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Globala
  • 103
  • 2
  • 14
  • 1
    debug that: through your program add lines that will output the json data received from the server. It is possible that steam's servers will block you because they don't want people messing with their stuff. – Phantom Apr 23 '15 at 12:58
  • 1
    possible duplicate of [Getting someone's Steam inventory](http://stackoverflow.com/questions/17393099/getting-someones-steam-inventory) – Andy Apr 23 '15 at 12:58
  • @Phantom It still outputs 'Success: True' so it's still getting the data from their servers. – Globala Apr 23 '15 at 12:59
  • 1
    @albin900 yes, but **what** data? Try `import pprint` and `pprint.pprint(json_data)` to see what you're actually getting, because it looks lke `json_data['rgDescriptions']` is empty. – jonrsharpe Apr 23 '15 at 13:00
  • @jonrsharpe I got two brackets [] any idea why? I also tried pprint.pprint(json_data['rgDescriptions'][0]) and it gave me the error 'out of range'... – Globala Apr 23 '15 at 13:04
  • `[]` means an empty list, which is why iterating over it (`for v in json_data['rgDescriptions']:`) isn't actually doing anything. As to **why** you aren't getting any items in that list; I don't know. @Phantom's suggestion seems likely, though. – jonrsharpe Apr 23 '15 at 13:14
  • @jonrsharpe LoL i tried to execute the exact same request in my browser and it turns out that the user didn't had any items. – Globala Apr 23 '15 at 13:19

3 Answers3

2

For anyone wondering how to do this now, this works for me in 2019:

#!/usr/bin/python
import json
import requests


def getInventory(steamid):
    data = requests.get(
        "https://steamcommunity.com/id/{}/inventory/json/730/2?l=english&count=5000".format(steamid))
    json_data = json.loads(data.text)
    descriptions = json_data["rgDescriptions"]
    print([(descriptions[item]["name"], getItemAmount(descriptions[item]["classid"], json_data)) for item in descriptions])


def getItemAmount(classid, json_data):
    inventory = json_data["rgInventory"]
    count = 0
    for item in inventory:
        if inventory[item]["classid"] == classid:
            count += 1
    return count

The new endpoint is https://steamcommunity.com/id/STEAMID/inventory/json/730/2

Luka
  • 21
  • 3
  • Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 24 '19 at 17:20
0

This following code works for me:

#!/usr/bin/python
import urllib2
import json

def getInventory(steamid):
    data = urllib2.urlopen('http://steamcommunity.com/profiles/'+steamid+'/inventory/json/730/2')
    json_data = json.loads(data.read())
    descriptions = json_data['rgDescriptions']
    print [descriptions[v]['name'] for v in descriptions]
    print('Done!')
    return

getInventory('76561197967150540');
Phantom
  • 378
  • 3
  • 19
Hispar
  • 1
  • 1
  • Looking at the steam id you are using, It has no rgDescriptions in it: http://steamcommunity.com/profiles/76561197988445370/inventory/json/730/2 – Hispar Apr 23 '15 at 14:24
0

This code is quite simple and it still works in 2022.

EDIT: You should use "market_hash_name" except "name". "name" might be changed by owner with nametag, when market_hash_name is always right and on point

import json

import requests


def parseInventory(steam64):
    json_data = (requests.get("https://steamcommunity.com/profiles/"+steam64+"/inventory/json/440/2")).json()
    descriptions = json_data["rgDescriptions"]
    for item in descriptions:
        name = descriptions[item]["market_hash_name"]
        print(name)

parseInventory("76561199336273908")
Osc4r
  • 126
  • 1
  • 7