从网站抓取的数据:
{'data': {'id': 1, 'name': 'Bitcoin', 'symbol': 'BTC', 'website_slug': 'bitcoin', 'rank': 1, 'circulating_supply': 17290912.0, 'total_supply': 17290912.0, 'max_supply': 21000000.0, 'quotes': {'USD': {'price': 6468.27571485, 'volume_24h': 4319636048.09599, 'market_cap': 111842386177.0, 'percent_change_1h': -0.31, 'percent_change_24h': 0.01, 'percent_change_7d': 0.76}}, 'last_updated': 1538037891}, 'metadata': {'timestamp': 1538037295, 'error': None}}
我无法访问price
变量,因为它嵌套在列表中。有没有办法在python中做到这一点?
我尝试过:
databs.append(data.get('USD', {}).get('price'))
但这返回None
完整代码:
c_list = ["1","52", "1958","1042"]
databs = []
def get_values():
t1 = time.time()
for i in c_list:
with urllib.request.urlopen("https://....") as url:
data = json.loads(url.read().decode())
#print(data) # This prints the list and i see the values are present
databs.append(data.get('USD', {}).get('price'))
a_current =databs[0]
b_current =databs[1]
c_current =databs[2]
d_current =databs[3]
print(databs)
while True:
get_values()
返回[None, None, None, None]
答案 0 :(得分:0)
要访问嵌套字典,您可以尝试执行以下操作:
def getDictValue(dic, key):
for item in key.split('.'):
if item in dic:
dic = dic[item]
else:
return None
return dic
要访问price
,例如:price = getDictValue(data, 'data.quotes.USD.price')