Home python How to handle getting a dictionary via Python Request

How to handle getting a dictionary via Python Request

Author

Date

Category

When making a request request, I have to get a dictionary in the dictionary. How can I access one of the keys now? In short, here’s the code:

response = requests.get (final_url)
skin = response.text
print (skin)

Print result:

{"iteminfo": {"origin": 4, "quality": 4, "rarity": 2, "a": "19937735378", "d": "17151644998212436232", "paintseed": 976, "defindex": 4, "paintindex": 799, "stickers": [], "floatid": "19763696990", " floatvalue ": 0.03432495519518852," m ":" 3305041656695931456 "," s ":" 0 "," imageurl ":" http://media.steampowered.com/apps/730/icons/econ/default_generated/weapon_glock_aa_vertigo_blue_blue_flue17clar3bblight_aa_vertigo_blue_blue_glock_aa_vertigo_blue_blue_glock_aa.png "," min ": 0," max ": 0.08," weapon_type ":" Glock-18 "," item_name ":" High Beam "," rarity_name ":" Industrial Grade "," quality_name ":" Unique " , "origin_name": "Crafted", "wear_name": "Factory New", "full_item_name": "Glock-18 | High Beam (Factory New)"}}

Perhaps I forgot something elementary, but the brain is just melting)
Tried it differently, it doesn’t work:

print (skin.get ('iteminfo'))
AttributeError: 'str' object has no attribute 'get'
print (response.get ('iteminfo'))
AttributeError: 'Response' object has no attribute 'get'

I have worked very little with dictionaries in this way, so I assume that the answer lies on the surface. I will be grateful to everyone who will help

P.S.

Thanks, the skin = response.json () code helped, but now for some reason I can’t get the dictionary key that is in the dictionary. I put in the following code:

response = requests.get (final_url)
skin = response.json ()
skin_iteminfo = skin.get ('iteminfo')
# paintseed = skin_iteminfo.get ('paintseed')
print (skin_iteminfo)

Everything works, there is a nested dictionary in the skin_iteminfo variable, that is, I just have to access any of its keys, but when I remove the comment and try to display one of the dictionary keys that is in the paintseed variable, I get an AttributeError: ‘NoneType’ object has no attribute ‘get’. How is this possible if I know that skin_iteminfo is a dictionary. I don’t understand what the error is.
I also tried this code:

paintseed = skin ['iteminfo'] ['paintseed']

but I get KeyError: ‘iteminfo’, although such a key exists


Answer 1

First question:

skin = response.json ()

Second question:

paintseed = skin ['iteminfo'] ['paintseed']

or

skin_iteminfo = skin.get ('iteminfo')
paintseed = skin_iteminfo.get ('paintseed')

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions