执行请求请求时,我应该会收到字典中的字典。我现在如何访问其中一个密钥?简而言之,这里是代码:
response = requests.get(final_url)
skin = response.text
print(skin)
打印结果:
{"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_light_large.0f4a3ec87faf17bb8557aa3b57a99606ac24c44e.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)"}}
也许我忘记了一些基本的东西,但大脑已经融化了)我尝试了不同的方式,它不起作用:
print(skin.get('iteminfo'))
AttributeError: 'str' object has no attribute 'get'
print(response.get('iteminfo'))
AttributeError: 'Response' object has no attribute 'get'
我很少以这种方式处理字典,所以我认为答案就在表面上。我会感谢所有帮助的人
附言
谢谢,代码 skin = response.json() 有所帮助,但现在由于某种原因我无法获得字典中的字典键。我发布了这段代码:
response = requests.get(final_url)
skin = response.json()
skin_iteminfo = skin.get('iteminfo')
# paintseed = skin_iteminfo.get('paintseed')
print(skin_iteminfo)
一切正常,skin_iteminfo 变量中有一个嵌套字典,也就是说,我只需要转到它的任何键,但是当我删除评论并尝试显示字典的键之一时,它在 paintseed变量,我得到错误 AttributeError: 'NoneType' object has no attribute 'get'。如果我知道 skin_iteminfo 是一本字典,这怎么可能。我不明白错误是什么。
也试过这段代码:
paintseed = skin['iteminfo']['paintseed']
但我收到错误 KeyError: 'iteminfo' 尽管存在这样的密钥
第一个问题:
第二个问题:
或者