从lastfm获取轨道的mbid:TypeError:字符串索引必须是整数

时间:2017-11-25 00:47:26

标签: python

我有轨道{},它使用最后一个fm api存储有关轨道的一些信息。现在我想将每个轨道的mbid存储在轨道{}中,但它出现错误:

  mbid = track['mbid']
TypeError: string indices must be integers

print(track)返回:

name
mbid
url
duration
....

print(track_mbid_response)的格式为:

{'track': {'name': 'Achtung Baby', 'mbid'.....: 

你知道问题出在哪里吗?

for i, v in tracks.items():
    artist = tracks[i]['artist'].replace(" ", "+")
    title = tracks[i]['title'].replace(" ", "+")


    track_mbid =  requests.get('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&api_key=' + api_key + '&artist=' + artist + '&track=' + title)
    track_mbid_response = track_mbid.json()

    #print(track_mbid_response)

    for track in track_mbid_response['track']:
        #print(track)

        mbid = track['mbid']
        print(mbid)

        tracks[ID]['mbid'] = tracks[i]['mbid']

1 个答案:

答案 0 :(得分:1)

您正在追踪轨道中的。只需执行track = track_mbid_response["track"],然后就可以添加到该词典中。