根据Python中的某个条件筛选获取的JSON数据

时间:2012-12-29 14:52:56

标签: python json musicbrainz

我是python的新手,我正在尝试使用urllib从musicbrainz获取JSON数据,并且能够解析一些数据。但是,对于某些艺术家来说,JSON数据中的某些字段/键不会出现,因为它们可能不是必需的。我不确定如何根据进一步的过滤条件过滤获取的JSON数据。

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

示例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

1 个答案:

答案 0 :(得分:0)

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 Service Search。 当然,您可以再次添加fmt=json。我刚删除了那部分,因为在浏览器中更容易阅读XML。


我还想注意,python library的当前版本有XML Web Service