Python 3.x 使用Spotipy和Python3的新手,我想使用New_releases()打印艺术家姓名和艺术家相册

Python 3.x 使用Spotipy和Python3的新手,我想使用New_releases()打印艺术家姓名和艺术家相册,python-3.x,api,spotify,spotipy,Python 3.x,Api,Spotify,Spotipy,到目前为止,我能够打印出所有相册的人,我选择使用这个 spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret)) results = spotify.artist_albums(posty_uri, album_type='album') albums = results['items'] while results['next']: re

到目前为止,我能够打印出所有相册的人,我选择使用这个

spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
results = spotify.artist_albums(posty_uri, album_type='album')
albums = results['items']
while results['next']:
    results = spotify.next(results)
    albums.extend(results['items'])
    
for album in albums:        
        print(album['name'])
我试图通过这样做为新的发布版本做一个类似的过程

newReleases = spotify.new_releases()
test = newReleases['items']

但是这在
test=newReleases['items']
行中给了我一个错误。如果有人熟悉Spotipy,并且知道如何从新发行版中返回发布日期、艺术家姓名、专辑名称()等信息,我将不胜感激。

我有点困惑,因为
new\u releases
方法会返回一个列表。在任何情况下,它都是一个包含列表的一项词典

然而,这个列表包含的字典看起来有点笨拙,所以我理解你为什么要问这个问题

您可以利用数据结构更容易地查看相关信息。我并不认为这是转换这些数据的最佳方式,但在我看来,这是一种体面的方式

import collecdtions as co 

# namedtuple data structure that will be easier to understand and use 
Album = co.namedtuple(typename='Album',field_names=['album_name',
                                                    'artist_name',
                                                    'release_date'])
newReleases2 = [] # couldn't think of a better name

for album in newReleases['albums']['items']: 
    artist_sublist = [] 
    for artist in album['artists']: 
        artist_sublist.append(artist['name'])
    newReleases2.append(Album(album_name=album['name'],
                              artist_name=artist_sublist,
                              release_date=album['release_date'])) 
这将产生以下命名偶列表:

[Album(album_name='Only Wanna Be With You (Pokémon 25 Version)', artist_name=['Post Malone'], release_date='2021-02-25'),
 Album(album_name='AP (Music from the film Boogie)', artist_name=['Pop Smoke'], release_date='2021-02-26'),
 Album(album_name='Like This', artist_name=['2KBABY', 'Marshmello'], release_date='2021-02-26'),
 Album(album_name='Go Big (From The Amazon Original Motion Picture Soundtrack Coming 2 America)', artist_name=['YG', 'Big Sean'], release_date='2021-02-26'),
 Album(album_name='Here Comes The Shock', artist_name=['Green Day'], release_date='2021-02-21'),
 Album(album_name='Spaceman', artist_name=['Nick Jonas'], release_date='2021-02-25'),
 Album(album_name='Life Support', artist_name=['Madison Beer'], release_date='2021-02-26'),
 Album(album_name="Drunk (And I Don't Wanna Go Home)", artist_name=['Elle King', 'Miranda Lambert'], release_date='2021-02-26'),
 Album(album_name='PROBLEMA', artist_name=['Daddy Yankee'], release_date='2021-02-26'),
 Album(album_name='Leave A Little Love', artist_name=['Alesso', 'Armin van Buuren'], release_date='2021-02-26'),
 Album(album_name='Rotate', artist_name=['Becky G', 'Burna Boy'], release_date='2021-02-22'),
 Album(album_name='BED', artist_name=['Joel Corry', 'RAYE', 'David Guetta'], release_date='2021-02-26'),
 Album(album_name='A N N I V E R S A R Y (Deluxe)', artist_name=['Bryson Tiller'], release_date='2021-02-26'),
 Album(album_name='Little Oblivions', artist_name=['Julien Baker'], release_date='2021-02-26'),
 Album(album_name='Money Long (feat. 42 Dugg)', artist_name=['DDG', 'OG Parker'], release_date='2021-02-26'),
 Album(album_name='El Madrileño', artist_name=['C. Tangana'], release_date='2021-02-26'),
 Album(album_name='Skegee', artist_name=['JID'], release_date='2021-02-23'),
 Album(album_name='Coyote Cry', artist_name=['Ian Munsick'], release_date='2021-02-26'),
 Album(album_name='Rainforest', artist_name=['Noname'], release_date='2021-02-26'),
 Album(album_name='The American Negro', artist_name=['Adrian Younge'], release_date='2021-02-26')]
如果您想查看与此列表中第11张专辑相关联的艺术家,可以执行以下操作:

In [62]: newReleases2[10].artist_name                                                               
Out[62]: ['Becky G', 'Burna Boy']

编辑:在对这个答案的评论中,OP要求获得专辑封面

请参见下面的帮助器函数和稍加修改的代码:

import os
import requests

def download_album_cover(url):
    # helper function to download album cover 
    # using code from: https://stackoverflow.com/a/13137873/42346
    download_path = os.getcwd() + os.sep + url.rsplit('/', 1)[-1] 
    r = requests.get(url, stream=True) 
    if r.status_code == 200: 
        with open(download_path, 'wb') as f: 
            for chunk in r.iter_content(1024): 
                f.write(chunk) 
        return download_path 

# modified data structure
Album = co.namedtuple(typename='Album',field_names=['album_name',
                                                    'album_cover',
                                                    'artist_name',
                                                    'release_date'])

# modified retrieval code
newReleases2 = []                                                                          

for album in newReleases['albums']['items']: 
    album_cover = download_album_cover(album['images'][0]['url']) 
    artist_sublist = []  
    for artist in album['artists']:  
        artist_sublist.append(artist['name']) 
    newReleases2.append(Album(album_name=album['name'], 
                              album_cover=album_cover, 
                              artist_name=artist_sublist, 
                              release_date=album['release_date'])) 
结果:

[Album(album_name='Scary Hours 2', album_cover='/home/adamcbernier/ab67616d0000b2738b20e4631fa15d3953528bbc', artist_name=['Drake'], release_date='2021-03-05'),
 Album(album_name='Boogie: Original Motion Picture Soundtrack', album_cover='/home/adamcbernier/ab67616d0000b27395e532805e8c97be7a551e3a', artist_name=['Various Artists'], release_date='2021-03-05'),
 Album(album_name='Hold On', album_cover='/home/adamcbernier/ab67616d0000b273f33d3618aca6b3cfdcd2fc43', artist_name=['Justin Bieber'], release_date='2021-03-05'),
 Album(album_name='Serotonin', album_cover='/home/adamcbernier/ab67616d0000b2737fb30ee0638c764d6f3247d2', artist_name=['girl in red'], release_date='2021-03-03'),
 Album(album_name='Leave The Door Open', album_cover='/home/adamcbernier/ab67616d0000b2736f9e6abbd6fa43ac3cdbeee0', artist_name=['Bruno Mars', 'Anderson .Paak', 'Silk Sonic'], release_date='2021-03-05'),
 Album(album_name='Real As It Gets (feat. EST Gee)', album_cover='/home/adamcbernier/ab67616d0000b273f0f6f6144929a1ff72001f5e', artist_name=['Lil Baby', 'EST Gee'], release_date='2021-03-04'),
 Album(album_name='Life’s A Mess II (with Clever & Post Malone)', album_cover='/home/adamcbernier/ab67616d0000b2732e8d23414fd0b81c35bdedea', artist_name=['Juice WRLD'], release_date='2021-03-05'),
 Album(album_name='slower', album_cover='/home/adamcbernier/ab67616d0000b273b742c96d78d9091ce4a1c5c1', artist_name=['Tate McRae'], release_date='2021-03-03'),
 Album(album_name='Sacrifice', album_cover='/home/adamcbernier/ab67616d0000b27398bfcce8be630dd5f2f346e4', artist_name=['Bebe Rexha'], release_date='2021-03-05'),
 Album(album_name='Poster Girl', album_cover='/home/adamcbernier/ab67616d0000b273503b16348e47bc3c1c823eba', artist_name=['Zara Larsson'], release_date='2021-03-05'),
 Album(album_name='Beautiful Mistakes (feat. Megan Thee Stallion)', album_cover='/home/adamcbernier/ab67616d0000b273787f41be59050c46f69db580', artist_name=['Maroon 5', 'Megan Thee Stallion'], release_date='2021-03-03'),
 Album(album_name='Pay Your Way In Pain', album_cover='/home/adamcbernier/ab67616d0000b273a1e1b4608e1e04b40113e6e1', artist_name=['St. Vincent'], release_date='2021-03-04'),
 Album(album_name='My Head is a Moshpit', album_cover='/home/adamcbernier/ab67616d0000b2733db806083e3b649f1d969a4e', artist_name=['Verzache'], release_date='2021-03-05'),
 Album(album_name='When You See Yourself', album_cover='/home/adamcbernier/ab67616d0000b27377253620f08397c998d18d78', artist_name=['Kings of Leon'], release_date='2021-03-05'),
 Album(album_name='Mis Manos', album_cover='/home/adamcbernier/ab67616d0000b273d7210e8d6986196b28d084ef', artist_name=['Camilo'], release_date='2021-03-04'),
 Album(album_name='Retumban2', album_cover='/home/adamcbernier/ab67616d0000b2738a79a82236682469aecdbbdf', artist_name=['Ovi'], release_date='2021-03-05'),
 Album(album_name='Take My Hand', album_cover='/home/adamcbernier/ab67616d0000b273b7839c3ba191de59f5d3a3d7', artist_name=['LP Giobbi'], release_date='2021-03-05'),
 Album(album_name="Ma' G", album_cover='/home/adamcbernier/ab67616d0000b27351b5ebb959c37913ac61b033', artist_name=['J Balvin'], release_date='2021-02-28'),
 Album(album_name='Aspen', album_cover='/home/adamcbernier/ab67616d0000b27387d1d17d16cf131765ce4be8', artist_name=['Young Dolph', 'Key Glock'], release_date='2021-03-05'),
 Album(album_name='Only The Family - Lil Durk Presents: Loyal Bros', album_cover='/home/adamcbernier/ab67616d0000b273a3df38e11e978b34b47583d0', artist_name=['Only The Family'], release_date='2021-03-05')]

非常感谢你!不管是不是最好的方法,我现在已经让它完美地工作了!我想做的最后一件事是尝试为每一个新发行的专辑封面。有什么想法吗?@Rogicar:干杯!对于专辑封面,我使用
requests
模块合并了一些快速图像下载代码。请参阅编辑后的答案。