Filter根据Python中的特定条件获取JSON数据



我是python的新手,我试图使用urllib从musicbrainz获取JSON数据,我能够解析一些数据。然而,对于一些艺术家来说,JSON数据中的某些字段/键没有出现,因为它们可能不是必需的。我不确定如何根据进一步的过滤标准过滤获取的JSON数据。

在下面的示例中,我将需要过滤获取的JSON数据,仅针对具有release-list>>release>>release-group>>类型为'Single'的数据。获取的JSON有时可以有多达50次迭代,但我只提供进一步过滤到类别为"Single"的JSON。请让我知道如何去做。谢谢!

示例JSON:

{
    "created": "2012-12-27T13:22:55.834Z",
    "recording-list": {
        "count": 3,
        "offset": 0,
        "recording": [{
            "score": "100",
            "title": "Stronger",
            "artist-credit": {
                "name-credit": [{
                    "artist": {
                        "name": "Britney Spears",
                        "sort-name": "Spears, Britney"
                    }
                }]
            },
            "release-list": {
                "release": [{
                    "id": "13c5511f-1f99-4ffe-97d5-562c05e9d8d5",
                    "title": "Hit Hammer 2001 (disc 1)",
                    "status": "Official",
                    "artist-credit": {
                        "name-credit": [{
                            "artist": {
                                "id": "89ad4ac3-39f7-470e-963a-56509c546377",
                                "name": "Various Artists"
                            }
                        }]
                    },
                      "release-group": {
                        "id": "6c4c2cc3-3d8e-3a19-9d46-da076c34b6e9",
                        "type": "Compilation",
                        "primary-type": "Album",
                        "secondary-type-list": {
                            "secondary-type": ["Compilation"]
                        }
                    },
                    "medium-list": {
                        "track-count": 20,
                        "medium": [{
                            "position": 1,
                            "track-list": {
                                "count": 20,
                                "offset": 0,
                                "track": [{
                                    "number": "1",
                                    "title": "Stronger",
                                    "length": 203266
                                }]
                            }
                        }]
                    }
                }]
            }
        }, {
            "id": "feb9acbf-1d3d-4395-9512-bfbdcfa72eb9",
            "score": "100",
            "title": "Stronger",
            "artist-credit": {
                "name-credit": [{
                    "joinphrase": "",
                    "artist": {
                        "name": "Britney Spears",
                        "sort-name": "Spears, Britney"
                    }
                }]
            },
            "release-list": {
                "release": [{
                    "id": "45e2a271-2f6b-4029-b11e-b6d94d169f9a",
                    "title": "Stronger: The Remixes",
                    "status": "Official",
                    "release-group": {
                        "id": "4d018ba8-f05e-4817-8c70-34307161a0fc",
                        "type": "Single",
                        "primary-type": "Single"
                    },
                    "date": "2000-12-12",
                    "country": "US",
                    "medium-list": {
                        "track-count": 6,
                        "medium": [{
                            "position": 1,
                            "format": "CD",
                            "track-list": {
                                "count": 6,
                                "offset": 0,
                                "track": [{
                                    "number": "1",
                                    "title": "Stronger",
                                    "length": 203000
                                }]
                            }
                        }]
                    }
                }]
            },
            "puid-list": {
                "puid": [{
                    "id": "28550845-c68a-314d-90c1-010dff730f4a"
                }]
            }
        }]
    }
}
Python代码:

def get_mbid(artist, song):
    artist=urllib.quote_plus(artist)
    song=urllib.quote_plus(song)
    recording_url = 'http://search.musicbrainz.org/ws/2/recording/?&fmt=json&query=artist:"'+artist+'"%20AND%20recording:"'+song+'"'
    search_results = urllib.urlopen(recording_url)
    json = simplejson.loads(search_results.read())
    search_results.close()
    if json['recording-list']['count'] == 0:
        return get_mbid_artist(artist)
    else:
        recordings = json['recording-list']['recording']
        for recording in recordings:
            mbid = recording['artist-credit']['name-credit'][0]['artist']['id']
            print mbid

http://musicbrainz.org/ws/2/recording?&query=artist%3A%22Britney+Spears%22+AND+recording%3A%22Stronger%22+AND+primarytype%3ASingle

将给您primarytype Single的录音。

没有编码:

artist:"Britney Spears" AND recording:"Stronger" AND primarytype:Single

参见:Web服务搜索。当然,您可以再次添加fmt=json。我只是删除了这一部分,因为它更容易在浏览器中读取XML。


我还想指出,有一个python库用于当前版本的XML Web服务。

相关内容

  • 没有找到相关文章

最新更新