Browse JSON with library python requests

0

In my example I want to access all the albums Coldplay has. This is the json. I understand that with the library requests I transform it to a dictionary but I can not access the albums but it returns the value of each position in the element topalbums link

import requests
import json

r = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=coldplay&api_key=a31007b9178e35854f9fa22039cb7362&format=json')
musica_dict = json.loads(r.text)

print type(musica_dict)

for item in musica_dict:
    for album in item:
        print album
    
asked by Diego 01.09.2018 в 00:02
source

1 answer

2

Your music_dict object is a dictionary, it contains a topalbums key, and if you access musica_dict ['topalbums'] it is also a dictionary which has two keys, album and @attr .

musica_dict ['topalbums'] [' album '] is a list (you can iterate), each item in this list is a dictionary that contains the ' name ',' playcount ',' mbid ',' url ',' artist ',' image '.

Then to print the information would be as follows:

for i in musica_dict['topalbums']['album']:
    print "Album: {}\nURL: {}".format(i['name'], i['url'])
    
answered by 19.10.2018 / 22:48
source