2016-10-04 5 views
0

Ich bin ein wenig verwirrt, wie Zugang artistsName in diesem json Ergebnis:Zugriff Artikel in JSON Ergebnis

{ 
    "tracks" : { 
    "href" : "https://api.spotify.com/v1/search?query=karma+police&offset=0&limit=20&type=track&market=BR", 
    "items" : [ { 
     "album" : { 
     "album_type" : "album", 
     "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ], 
     "external_urls" : { 
      "spotify" : "https://open.spotify.com/album/7dxKtc08dYeRVHt3p9CZJn" 
     }, 
     "href" : "https://api.spotify.com/v1/albums/7dxKtc08dYeRVHt3p9CZJn", 
     "id" : "7dxKtc08dYeRVHt3p9CZJn", 
     "images" : [ { 
      "height" : 640, 
      "url" : "https://i.scdn.co/image/f89c1ecdd0cc5a23d5ad7303d4ae231d197dde98", 
      "width" : 640 
     }, { 
      "height" : 300, 
      "url" : "https://i.scdn.co/image/1b898f0b8e3ce499d0fc629a1918c144d982e475", 
      "width" : 300 
     }, { 
      "height" : 64, 
      "url" : "https://i.scdn.co/image/faf295a70a6531826a8c25d33aad7d2cd9c75c7a", 
      "width" : 64 
     } ], 
     "name" : "OK Computer", 
     "type" : "album", 
     "uri" : "spotify:album:7dxKtc08dYeRVHt3p9CZJn" 
     }, 
     "artists" : [ { 
     "external_urls" : { 
      "spotify" : "https://open.spotify.com/artist/4Z8W4fKeB5YxbusRsdQVPb" 
     }, 
     "href" : "https://api.spotify.com/v1/artists/4Z8W4fKeB5YxbusRsdQVPb", 
     "id" : "4Z8W4fKeB5YxbusRsdQVPb", 
     "name" : "Radiohead", 
     "type" : "artist", 
     "uri" : "spotify:artist:4Z8W4fKeB5YxbusRsdQVPb" 
     } ], 

Ausgangsform: results = sp.search(q=track, type='track'),

ich items = results['tracks']['items']['artists']['name'] habe versucht, keine in Anspruch nehmen.

kann jemand bitte helfen?

EDIT: Dieser Teil meines print (result) ist:

{u'tracks': {u'items': [{u'album': {u'album_type': u'single', u'name': u"Told You I'd Be with the Guys", u'external_urls': {u'spotify': u'https://open.spotify.com/album/5T3yXvWzj9LOFjUNu3s6Sp'}, u'uri': u'spotify:album:5T3yXvWzj9LOFjUNu3s6Sp', u'href': u'https://api.spotify.com/v1/albums/5T3yXvWzj9LOFjUNu3s6Sp', u'images': [{u'url': u'https://i.scdn.co/image/520d8039048ea52c917f73360983678b7699e48e', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/ab137f85737b9109944bbd65b73d13cb9969fc1c', u'width': 300, u'height': 300}, {u'url': u'https://i.scdn.co/image/49018040279b8f68ff74fc940257e6ced4c72508', u'width': 64, u'height': 64}], u'type': u'album', u'id': u'5T3yXvWzj9LOFjUNu3s6Sp', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}, u'name': u"Told You I'd Be with the Guys", u'uri': u'spotify:track:2GeIr050NcweBQzsabIYfB', u'external_urls': {u'spotify': u'https://open.spotify.com/track/2GeIr050NcweBQzsabIYfB'}, u'popularity': 45, u'explicit': False, u'preview_url': u'https://p.scdn.co/mp3-preview/16e7af443fa59b8be61385ca71d5f76c7a7f1f8a', u'track_number': 1, u'disc_number': 1, u'href': u'https://api.spotify.com/v1/tracks/2GeIr050NcweBQzsabIYfB', u'artists': [{u'name': u'Cherry Glazerr', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/3pIGm1omCcHIb1juBNHspg'}, u'uri': u'spotify:artist:3pIGm1omCcHIb1juBNHspg', u'href': u'https://api.spotify.com/v1/artists/3pIGm1omCcHIb1juBNHspg', u'type': u'artist', u'id': u'3pIGm1omCcHIb1juBNHspg'}], u'duration_ms': 273946, u'external_ids': {u'isrc': u'US38W1634501'}, u'type': u'track', u'id': u'2GeIr050NcweBQzsabIYfB', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}], u'next': None, u'href': u'https://api.spotify.com/v1/search?query=Told+You+I%27d+Be+With+the+Guys&offset=0&limit=10&type=track', u'limit': 10, u'offset': 0, u'total': 1, u'previous': None}} 2016-10-04 11:55:05 [requests.packages.urllib3.connectionpool] INFO: Starting new HTTPS connection (1): api.spotify.com 2016-10-04 11:55:06 [requests.packages.urllib3.connectionpool] DEBUG: "GET /v1/search?q=Blood+on+Me&limit=10&type=track&offset=0 HTTP/1.1" 200 None {u'tracks': {u'items': [{u'album': {u'album_type': u'single', u'name': u'Blood On Me', u'external_urls': {u'spotify': u'https://open.spotify.com/album/43hQRnuVUstKeV2uc3DJXv'}, u'uri': u'spotify:album:43hQRnuVUstKeV2uc3DJXv', u'href': u'https://api.spotify.com/v1/albums/43hQRnuVUstKeV2uc3DJXv', u'images': [{u'url': u'https://i.scdn.co/image/a9d6864491cf8c544bd53f532cdc265b3d7ffd77', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/8834ac4ea759929a5cb9c80505a30672cce0daaa', u'width': 300, u'height': 300}, {u'url': u'https://i.scdn.co/image/ef39d8bdc8df5915679a97b6b8fb6295f9e45bdd', u'width': 64, u'height': 64}], u'type': u'album', u'id': u'43hQRnuVUstKeV2uc3DJXv', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'JP', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}, u'name': u'Blood On Me', u'uri': u'spotify:track:4JJIj448WHVYw2kEYfIj94', u'external_urls': {u'spotify': u'https://open.spotify.com/track/4JJIj448WHVYw2kEYfIj94'}, u'popularity': 61, u'explicit': False, u'preview_url': u'https://p.scdn.co/mp3-preview/a217661593e88e4c3363e5bb48f9b3668be36cb3', u'track_number': 1, u'disc_number': 1, u'href': u'https://api.spotify.com/v1/tracks/4JJIj448WHVYw2kEYfIj94', u'artists': [{u'name': u'Sampha', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2WoVwexZuODvclzULjPQtm'}, u'uri': u'spotify:artist:2WoVwexZuODvclzULjPQtm', u'href': u'https://api.spotify.com/v1/artists/2WoVwexZuODvclzULjPQtm', u'type': u'artist', u'id': u'2WoVwexZuODvclzULjPQtm'}], u'duration_ms': 246951, u'external_ids': {u'isrc': u'UK7MC1600028'}, u'type': u'track', u'id': u'4JJIj448WHVYw2kEYfIj94', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'JP', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}, {u'album': {u'album_type': u'album', u'name': u'Blood On The Tracks', u'external_urls': {u'spotify': u'https://open.spotify.com/album/4WD4pslu83FF6oMa1e19mF'}, u'uri': u'spotify:album:4WD4pslu83FF6oMa1e19mF', u'href': u'https://api.spotify.com/v1/albums/4WD4pslu83FF6oMa1e19mF', u'images': [{u'url': u'https://i.scdn.co/image/2facdd8d670a99db33fe829ea917ef95f526a5cc', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/1544214291d2b37d2aad603941e3f89eb05a1f2e', u'width': 300, u'height': 300}, {u'url': u'https://i.scdn.co/image/1372d865672d6c97048fdb151ed49586718cfa01', u'width': 64, u'height': 64}], u'type': u'album', u'id': u'4WD4pslu83FF6oMa1e19mF', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'JP', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}, u'name': u'Meet Me in the Morning', u'uri': u'spotify:track:53ygARQf1f30Z0EmXPHWGT', u'external_urls': {u'spotify': u'https://open.spotify.com/track/53ygARQf1f30Z0EmXPHWGT'}, u'popularity': 45, u'explicit': False, u'preview_url': u'https://p.scdn.co/mp3-preview/9331724efc1c504580759f192699d78d4f6cba44', u'track_number': 6, u'disc_number': 1, u'href': u'https://api.spotify.com/v1/tracks/53ygARQf1f30Z0EmXPHWGT', u'artists': [{u'name': u'Bob Dylan', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/74ASZWbe4lXaubB36ztrGX'}, u'uri': u'spotify:artist:74ASZWbe4lXaubB36ztrGX', u'href': u'https://api.spotify.com/v1/artists/74ASZWbe4lXaubB36ztrGX', u'type': u'artist', u'id': u'74ASZWbe4lXaubB36ztrGX'}], u'duration_ms': 258973, u'external_ids': {u'isrc': u'USSM19906473'}, u'type': u'track', u'id': u'53ygARQf1f30Z0EmXPHWGT', u'available_markets': [u'AD', u'AR', u'AT', u'AU', u'BE', u'BG', u'BO', u'BR', u'CA', u'CH', u'CL', u'CO', u'CR', u'CY', u'CZ', u'DE', u'DK', u'DO', u'EC', u'EE', u'ES', u'FI', u'FR', u'GB', u'GR', u'GT', u'HK', u'HN', u'HU', u'ID', u'IE', u'IS', u'IT', u'JP', u'LI', u'LT', u'LU', u'LV', u'MC', u'MT', u'MX', u'MY', u'NI', u'NL', u'NO', u'NZ', u'PA', u'PE', u'PH', u'PL', u'PT', u'PY', u'SE', u'SG', u'SK', u'SV', u'TR', u'TW', u'US', u'UY']}, {u'album': {u'album_type': u'album', u'name': u'Blood On The Tracks', u'external_urls':

+0

Sie wahrscheinlich dieses heraus sich am REPL mit einiger Zeit Abbildung könnte. Wenn Sie versuchen, 'result ['tracks'] ['items'] ['artists'] ['name']', dann 'results ['tracks'] ['items'] ['artists']' und so zu drucken Bis Sie einen Fehler erhalten, können Sie genau sehen, in welches Datenelement Sie sich nicht indizieren können, und es untersuchen (zum Beispiel seinen Typ überprüfen; wenn der Typ "Liste" statt "dict" ist, dann sollte es sein offensichtlich, dass Sie nicht mit dem Namen indizieren können). –

Antwort

0

Defensiv geschrieben, das etwas aussehen könnte:

artist_names = set() # using a set avoids adding the same name more than once 
if 'tracks' in results and 'items' in results['tracks']: 
    for item in results['tracks']['items']: 
    for artist in item.get('artists',()): 
     if 'name' in artist: 
     artist_names.add(artist['name']) 

for name in artist_names: 
    print "Found artist:", name 
+0

nett. Könntest du es bitte editieren, so dass es 'track ids' ausgibt (erhalten von' t_items = result ['tracks'] ['items'] 'und dann' für t in t_items: track_ids.append (t ['uri']) '' Mein letztes Ziel ist es, Tracks zu holen, die diesen Künstlern entsprechen –

+1

@data_garden, insofern diese Anfrage nicht innerhalb des Inhalts Ihrer Frage enthalten ist (oder sogar impliziert wird), wäre es unangemessen für mich, sie einer hinzuzufügen antwort. –

+0

@fair genug, danke –

0

Sie sollten items = results['tracks']['items'][0]['artists'][0]['name'] tun (obwohl dies technisch fest einprogrammiert ist)

Als Ihre Künstler und Gegenstände Objekt-Arrays (mit nur einem, so verweisen wir die erste mit [0])

+0

Außer dass dies Python ist, nicht Javascript. –

+0

Whoops. aktualisiert. Prost. –

1

artists wird eine Liste von dicts, weil eine Spur kann ha viele Künstler. Ähnlich ist items auch eine Liste. So können Sie zum Beispiel results['tracks']['items'][0]['artists'][0]['name'] machen, aber das wird Ihnen nur den ersten Künstler des ersten Tracks bringen.

+0

Ich bekomme 'IndexError: Listenindex außerhalb des Bereichs '. Macht das Sinn? –

+1

@data_garden, das macht Sinn, wenn Sie eine Spur ohne Elemente oder ein Element ohne Künstler haben. –