Ich bekomme die Top-Künstler aus einem bestimmten Land von lastfm api. Im immer den Namen des einzelnen Künstler und Ladens in den artists{}
mit Erfolg:Erste Top-Alben von last.fm
import requests
api_key = "0929f1144e531702a5563c887cbbade0"
ID = 0
artists = {}
for i in range(1, 3):
artists_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&page=' + str(i) + '&api_key=' + api_key)
artists_data = artists_response.json()
for artist in artists_data["topartists"]["artist"]:
name = artist["name"]
artists[ID] = {}
artists[ID]['ID'] = ID
artists[ID]['name'] = name
ID += 1
Aber jetzt für jeden Künstler möchte ich die top5 Alben und Laden in den albums{},
die Künstlernamen bekommen, der Künstler-ID, der Name das Album und die URL des Albums.
Zum Beispiel für jeden Künstler, ich mag, wie dies für jedes Album der top5 Alb einen Datensatz erhalten:
"Artist": "U2", "ID": 175, "ArtistID": 10, "Title": Achtung Baby",
"Image": "https://lastfm-img2.akamaized.net/i/u/174s/a482040d21924ddacd5fe75dedbb1ef2.png"},
"URL": "https://www.last.fm/music/U2/Achtung+Baby"}, "487": {"Date": "2004-01-01"
"Artist": "U2", "ID": 176, "ArtistID": 10, "Description": "None", "Title": "Boy",
... and then the same for the other 3 albums of the top5
Im dies unten mit dem Code zu tun, aber es ist nicht richtig funktioniert. Die artistId erscheint immer als "0" und es erscheinen nur die 5 Alben des ersten Künstlers. Weißt du, wo das Problem ist?
albums = {}
for i,v in artists.items():
chosen = artists[i]['name'].replace(" ", "+")
topalbums_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&format=json&artist=' + chosen + '&api_key=' + api_key + '&limit=5')
albums_data = topalbums_response.json()
for album in albums_data['topalbums']['album']:
name = album["name"]
url = album["url"]
albums[ID] = {}
albums[ID]['ID'] = ID
albums[ID]['artist'] = artists[i]['name']
albums[ID]['artistID'] = artists[i]['ID']
albums[ID]['name'] = name
albums[ID]['url'] = url
for image in album['image']:
if (image["size"] == "large"):
if (image["size"] is not None):
image = image["#text"]
albums[ID]['image'] = image
ID += 1
print(albums)
Danke, aber es funktioniert nicht. Die artistID erscheint nun, aber dann erscheinen die Alben, die einem Künstler zugeordnet sind, von einem anderen Künstler und auch nicht alle Ergebnisse, nur eins. – OzzyW
hat der Antwort eine Bearbeitung hinzugefügt, dass die ID vor der Artisten-Schleife initialisiert wird, aber nicht vor der Loop-Schleife. –
Ich habe "ID = 0" vor "albuns = {}" hinzugefügt, aber ich bekomme das gleiche Problem. – OzzyW