读取 JSON 文件以获得最高分辨率的图像 (Last.FM)

问题描述

因此,我正在开发一个项目,该项目会在每次特定日期(例如“周一 22:00:00”)时发送一条 discord 消息。该消息包括我那周最常听的专辑。每当我获得访问 JSON 的 URL 时,我都会使代码正常工作,其中包含多个指向图像的链接。这是我得到的 JSON 响应:

{
    "album": {
        "name": "Significant Other","artist": "Limp Bizkit","mbid": "be3e00aa-368a-3f09-ac96-cd094e9a7151","url": "https://www.last.fm/music/Limp+Bizkit/Significant+Other","image": [{
            "#text": "https://lastfm.freetls.fastly.net/i/u/34s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png","size": "small"
        },{
            "#text": "https://lastfm.freetls.fastly.net/i/u/64s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png","size": "medium"
        },{
            "#text": "https://lastfm.freetls.fastly.net/i/u/174s/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png","size": "large"
        },{
            "#text": "https://lastfm.freetls.fastly.net/i/u/300x300/1c00f7b9cd94c2b6fbd7f12fc00bd8d2.png","size": "extralarge"
        },"size": "mega"
        },"size": ""
        }],"listeners": "774409","playcount": "10274552","tracks": {
            "track": [{
                "name": "Intro","url": "https://www.last.fm/music/Limp+Bizkit/_/Intro","duration": "78","@attr": {
                    "rank": "1"
                },"streamable": {
                    "#text": "0","fulltrack": "0"
                },"artist": {
                    "name": "Limp Bizkit","mbid": "8f9d6bb2-dba4-4cca-9967-cc02b9f4820c","url": "https://www.last.fm/music/Limp+Bizkit"
                }
            },{
                "name": "Just Like This","url": "https://www.last.fm/music/Limp+Bizkit/_/Just+Like+This","duration": "215","@attr": {
                    "rank": "2"
                },{
                "name": "Nookie","url": "https://www.last.fm/music/Limp+Bizkit/_/Nookie","duration": "289","@attr": {
                    "rank": "3"
                },{
                "name": "Break Stuff","url": "https://www.last.fm/music/Limp+Bizkit/_/Break+Stuff","duration": "166","@attr": {
                    "rank": "4"
                },{
                "name": "Re-Arranged","url": "https://www.last.fm/music/Limp+Bizkit/_/Re-Arranged","duration": "354","@attr": {
                    "rank": "5"
                },{
                "name": "I'm broke","url": "https://www.last.fm/music/Limp+Bizkit/_/I%27m+broke","duration": "239","@attr": {
                    "rank": "6"
                },{
                "name": "Nobody Like You","url": "https://www.last.fm/music/Limp+Bizkit/_/Nobody+Like+You","duration": "260","@attr": {
                    "rank": "7"
                },{
                "name": "Don't Go Off Wandering","url": "https://www.last.fm/music/Limp+Bizkit/_/Don%27t+Go+Off+Wandering","@attr": {
                    "rank": "8"
                },{
                "name": "9 Teen 90 Nine","url": "https://www.last.fm/music/Limp+Bizkit/_/9+Teen+90+Nine","duration": "276","@attr": {
                    "rank": "9"
                },{
                "name": "N 2 Gether Now","url": "https://www.last.fm/music/Limp+Bizkit/_/N+2+Gether+Now","@attr": {
                    "rank": "10"
                },{
                "name": "Trust?","url": "https://www.last.fm/music/Limp+Bizkit/_/Trust%3F","duration": "299","@attr": {
                    "rank": "11"
                },{
                "name": "No Sex","url": "https://www.last.fm/music/Limp+Bizkit/_/No+Sex","duration": "234","@attr": {
                    "rank": "12"
                },{
                "name": "Show Me What You Got","url": "https://www.last.fm/music/Limp+Bizkit/_/Show+Me+What+You+Got","duration": "266","@attr": {
                    "rank": "13"
                },{
                "name": "A Lesson Learned","url": "https://www.last.fm/music/Limp+Bizkit/_/A+Lesson+Learned","duration": "160","@attr": {
                    "rank": "14"
                },{
                "name": "Outro / Radio Sucks / The Mind of Les","url": "https://www.last.fm/music/Limp+Bizkit/_/Outro+%2F+Radio+Sucks+%2F+The+Mind+of+Les","duration": "438","@attr": {
                    "rank": "15"
                },{
                "name": "[silence]","url": "https://www.last.fm/music/Limp+Bizkit/_/%5Bsilence%5D","duration": "4","@attr": {
                    "rank": "16"
                },"url": "https://www.last.fm/music/Limp+Bizkit"
                }
            }]
        },"tags": {
            "tag": [{
                "name": "albums I own","url": "https://www.last.fm/tag/albums+I+own"
            },{
                "name": "Nu Metal","url": "https://www.last.fm/tag/Nu+Metal"
            },{
                "name": "rapcore","url": "https://www.last.fm/tag/rapcore"
            },{
                "name": "rock","url": "https://www.last.fm/tag/rock"
            },{
                "name": "limp bizkit","url": "https://www.last.fm/tag/limp+bizkit"
            }]
        },"wiki": {
            "published": "11 Jul 2016,20:19","summary": "Significant Other is the second album by American rap rock/nu Metal band Limp Bizkit. Released in 1999 by Flip/Interscope Records,the album saw the band expanding its sound from that of its debut album Three Dollar Bill,Yall,to incorporate further Metal and hip hop influences. Significant Other was co-produced by Terry Date and Limp Bizkit. The album has sold at least 16 million copies worldwide. <a href=\"http://www.last.fm/music/Limp+Bizkit/Significant+Other\">Read more on Last.fm</a>.","content": "Significant Other is the second album by American rap rock/nu Metal band Limp Bizkit. Released in 1999 by Flip/Interscope Records,to incorporate further Metal and hip hop influences. Significant Other was co-produced by Terry Date and Limp Bizkit. The album has sold at least 16 million copies worldwide. <a href=\"http://www.last.fm/music/Limp+Bizkit/Significant+Other\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."
        }
    }
}

我想获取 "size": "" 上方的链接,因为此图像具有最高分辨率。我如何才能做到这一点?我用 Python 制作了 discord 机器人,所以如果你能用 Python 语言解释,那就太好了!提前致谢!

解决方法

要将 JSON 字符串转换为 Python 对象,您可以使用:

import json
album_data = json.loads(album_json_string)

要提取“大型”图像 URL,您可以使用列表推导式遍历图像列表:

images = album_data['album']['image']
mega_images = [i for i in images if i['size'] == 'mega']
try:
    mega_image_url = mega_images[0]['image']
except IndexError:
    #do something if the mega URL isn't present