2017-11-19 3 views
0

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) 

Antwort

0

Try Album Schleife Wechsel wie folgt zu starten:

for _, v in artists.items(): 
    chosen = v['name'].replace(" ", "+") 

Außerdem merke ich, Sie ID = 0 zu Beginn der Künstler zuweisen, aber es innerhalb der Alben verweisen, möchten Sie vielleicht darauf schauen.

==== EDIT:

Der folgende Code funktioniert für mich, obwohl ich nicht wirklich sicher bin, welches Format Sie so ist es unvollständig erhalten möchten.

import requests 

api_key = "0929f1144e531702a5563c887cbbade0" 

artists_url = (
    'http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists' 
    '&country=spain&format=json&page={}&api_key={}' 
) 

artists = [] 

for page in range(1, 3): 
    artists_data = requests.get(artists_url.format(str(page), api_key)).json() 
    for ID, artist in enumerate(artists_data["topartists"]["artist"]): 
     artists.append((ID, artist["name"])) 


albums = {} 
albums_url = (
    'http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums' 
    '&format=json&artist={}&api_key={}&limit=5' 
) 


for ID, artist in artists[:3]: ### LIMITED to first three artists for testing. 
    print('\n------\n{} {}'.format(ID, artist)) 
    chosen = artist.replace(" ", "+") 
    albums_data = requests.get(albums_url.format(chosen, api_key)).json() 

    for album in albums_data['topalbums']['album']: 

     name = album["name"] 
     url = album["url"] 
     print('{}\n{}'.format(name, url)) 
     print(album) 
     print() 
#  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) 
#  print() 
+0

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

+0

hat der Antwort eine Bearbeitung hinzugefügt, dass die ID vor der Artisten-Schleife initialisiert wird, aber nicht vor der Loop-Schleife. –

+0

Ich habe "ID = 0" vor "albuns = {}" hinzugefügt, aber ich bekomme das gleiche Problem. – OzzyW