【问题标题】:Getting top albums from last.fm从 last.fm 获取热门专辑
【发布时间】:2017-11-19 01:15:04
【问题描述】:

我正在从 lastfm api 获取来自特定国家/地区的顶级艺术家。我在artists{} 中成功获取了每位艺术家的姓名并存储:

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

但是现在对于每个艺术家,我想获取前 5 名专辑并在 albums{}, 中存储艺术家姓名、艺术家 ID、专辑名称和专辑网址。

例如,对于每个艺术家,我想为 top5 专辑中的每张专辑获取一条记录,如下所示:

 "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

我正在使用下面的代码执行此操作,但它无法正常工作。艺术家 ID 始终显示为“0”,并且仅显示第一位艺术家的 5 张专辑。你知道问题出在哪里吗?

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)

【问题讨论】:

    标签: python last.fm


    【解决方案1】:

    试着改变你的专辑循环,像这样开始:

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

    此外,我注意到您在艺术家的开头分配了 ID = 0,但在专辑中引用它,您可能想要查看它。

    ==== 编辑:

    以下代码对我有用,虽然我不太确定你想获得什么格式,所以它不完整。

    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()
    

    【讨论】:

    • 谢谢,但它不起作用。现在出现了艺术家 ID,但随后出现的与某个艺术家关联的专辑来自其他艺术家,而且它也没有返回所有结果,只返回一个。
    • 添加了一个关于您在艺术家循环之前而不是在专辑循环之前启动 ID 的答案的编辑。
    • 我在“albuns = {}”之前添加了“ID = 0”,但遇到了同样的问题。
    • 我发布了我修改后的代码版本。请注意,我使用的是枚举,而不是手动生成 ID 号。另外,我将艺术家列在一个列表中。
    • 谢谢.. 但是你知道为什么当到达 count/id 等于 49 时它返回 0 而不是 50 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多