Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - 'NoneType' object is not subscriptable (A Small Program For Steam Item Price)

Tags:

python

steam

So , i made this small program to update about the lowest_price of a specific item on steam market , it runs a loop and gets json response.

It works fine initially , displays the price , but after a while it shows the error.

The program code :

import json
import requests

def GetPrice () :

    response = requests.get ('https://steamcommunity.com/market/priceoverview/?appid=264710&currency=1&market_hash_name=Planet%204546B%20Postcard')

    json_data = {}
    json_data = json.loads (response.text)

    return json_data ["lowest_price"]

while True :

    print (GetPrice ())

Here is the output of the program :

$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
$1.03
Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\item_price.py", line 16, in <module>
    print (GetPrice ())
  File "C:\Users\Admin\Desktop\item_price.py", line 12, in GetPrice
    return json_data ["lowest_price"]
TypeError: 'NoneType' object is not subscriptable
[Finished in 20.2s]
like image 966
KING Avatar asked Jul 06 '26 18:07

KING


2 Answers

This error occurs when you are trying to index an object that is of type None (i.e: the object has no value).

Here your None object is your json_data variable, which means that json.loads (response.text) returns None.

You can avoid this error by adding an if statement that will check if the value is not None:

if json_data is not None:
    return json_data['lowest_price']
return None

Or with a try-except statement:

try:
    return json_data['lowest_price']
except Exception as e:
    return None    # or you can raise an exception if you want
like image 137
Noé Mastrorillo Avatar answered Jul 09 '26 07:07

Noé Mastrorillo


You're experiencing this problem since you're making too many requests to the server really fast.

To be specific, the server replies with http error code 429

Consider waiting for a few seconds before sending successive requests.

like image 29
Balaji Ambresh Avatar answered Jul 09 '26 08:07

Balaji Ambresh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!