0

I am trying to print the number after quantity in the following JSON:

app_data : {
    quantity: 1,
    ...
    ...
}

This is the link where I am trying to print

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), options=chrome_options)
inv = "https://steamcommunity.com/profiles/76561198404652782/inventory/json/440/2"
with urllib.request.urlopen(inv) as url:
    data = json.loads(url.read().decode())
    result = data.find('quantity')
    print(data, result)
    print(data)

Also tried .find() but no success

Veer Shrivastav
  • 5,434
  • 11
  • 53
  • 83
perzsa
  • 25
  • 5

2 Answers2

1

json.loads() returns a dictionary, and a dictionary does not have a find() method on it. Also, what the request returns is a nested dictionary, so a direct key lookup won't work. You may have to try something like what's been suggested in these earlier posts.

Find all occurrences of a key in nested python dictionaries and lists

How can I search for specific keys in this nested dictionary in Python?

navneethc
  • 1,234
  • 8
  • 17
-1

You are searching for a key, So just use a condition for it..

if 'quantity' in data:
  print data['quantity']
Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29