I am a bit confused as to how access artists
name in this json
result:
{
"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"
} ],
starting form : results = sp.search(q=track, type='track')
,
I've tried items = results['tracks']['items']['artists']['name']
, to no avail.
can someone please help?
EDIT: this is part of my print (result)
:
{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 :(得分:1)
items
是一个列表的dicts,因为一个曲目可以有很多艺术家。同样地,results['tracks']['items'][0]['artists'][0]['name']
也是一个列表。例如,您可以{{1}},但这只会让您成为第一首曲目的第一位艺术家。
答案 1 :(得分:0)
你应该做items = results['tracks']['items'][0]['artists'][0]['name']
(虽然这在技术上是硬编码的)
由于你的艺术家和物品对象是数组(只有一个,所以我们用[0]引用第一个)
答案 2 :(得分:0)
防御性写作,这可能看起来像:
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