解析 python 字典时出现键错误

key error while parsing python dictionary

{
"kind": "youtube#commentThreadListResponse",
"etag": "5b1YCNidguUpH4QsR6mpPJrL6es",
"nextPageToken": "QURTSl9pMTQwTEZFU1VRZTB1R2toTFh5djJJSWQzM1oyOXp4Z3ppSXZSNEtNQ25RRzQyRm1xXzFwMDZvc3dqb1g5dnQyTnVUMVJld2lWVXFta2tFclh2LWk3eENwOFFxMmluTGhlY3JXOHNsSnh4ZlFyNllfdWVWMVlPdkhiWWlnVzA=",
"pageInfo": {
    "totalResults": 100,
    "resultsPerPage": 100
},
"items": [
    {
        "kind": "youtube#commentThread",
        "etag": "GQifP0HFLluusa1n0pFQCxggSvI",
        "id": "UgxWDLFO6d6fhe4UaJd4AaABAg",
        "snippet": {
            "videoId": "BEWz4SXfyCQ",
            "topLevelComment": {
                "kind": "youtube#comment",
                "etag": "YlbdyUbeN1LqFBOqDnQnQZU2DnQ",
                "id": "UgxWDLFO6d6fhe4UaJd4AaABAg",
                "snippet": {
                    "videoId": "BEWz4SXfyCQ",
                    "textDisplay": "Honestly Jeremy is just an annoying piggyback rider",
                    "textOriginal": "Honestly Jeremy is just an annoying piggyback rider",
                    "authorDisplayName": "Michael Myers",
                    "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJwHIfrPXguIZR7YggVntreixLfBisGtlo5xTg=s48-c-k-c0xffffffff-no-rj-mo",
                    "authorChannelUrl": "http://www.youtube.com/channel/UCs4do_iNqxBcxxPmv6U1VPg",
                    "authorChannelId": {
                        "value": "UCs4do_iNqxBcxxPmv6U1VPg"
                    },
                    "canRate": true,
                    "viewerRating": "none",
                    "likeCount": 0,
                    "publishedAt": "2020-07-08T20:55:48Z",
                    "updatedAt": "2020-07-08T20:55:48Z"
                }
            },
            "canReply": true,
            "totalReplyCount": 0,
            "isPublic": true
        }
    },

    {
        "kind": "youtube#commentThread",
        "etag": "wFEgumlYzFR2ZLOsHgEdQoV45SI",
        "id": "UgxaQ38-nL84EgK9ABh4AaABAg",
        "snippet": {
            "videoId": "BEWz4SXfyCQ",
            "topLevelComment": {
                "kind": "youtube#comment",
                "etag": "KyMK87Zq9ej2AHtl44x5-ykwnzQ",
                "id": "UgxaQ38-nL84EgK9ABh4AaABAg",
                "snippet": {
                    "videoId": "BEWz4SXfyCQ",
                    "textDisplay": "Bring bob back and leave captain graybeard at the damn house",
                    "textOriginal": "Bring bob back and leave captain graybeard at the damn house",
                    "authorDisplayName": "Brad Johnson",
                    "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJzzXxTu9bz5hzGL20X1w3ALIcqIWBCc4uzuQPS8=s48-c-k-c0xffffffff-no-rj-mo",
                    "authorChannelUrl": "http://www.youtube.com/channel/UCwTUCnELUJ3IwcBsEwqjNaQ",
                    "authorChannelId": {
                        "value": "UCwTUCnELUJ3IwcBsEwqjNaQ"
                    },
                    "canRate": true,
                    "viewerRating": "none",
                    "likeCount": 1,
                    "publishedAt": "2020-07-08T18:37:35Z",
                    "updatedAt": "2020-07-08T18:37:35Z"
                }
            },
            "canReply": true,
            "totalReplyCount": 1,
            "isPublic": true
        },
        "replies": {
            "comments": [
                {
                    "kind": "youtube#comment",
                    "etag": "eEq9MZRmGGq3sX4IpzEHk_pYvTw",
                    "id": "UgxaQ38-nL84EgK9ABh4AaABAg.9ArZ6N2FniS9ArdOylLUcm",
                    "snippet": {
                        "videoId": "BEWz4SXfyCQ",
                        "textDisplay": "No, because then there'd be no one to distract you from what a fraud Lazar is.",
                        "textOriginal": "No, because then there'd be no one to distract you from what a fraud Lazar is.",
                        "parentId": "UgxaQ38-nL84EgK9ABh4AaABAg",
                        "authorDisplayName": "Rombert Dillahuntsvalle",
                        "authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJwALDysFZlmZoXLVeqzSZc6HcvUetsOCk6a2vTY=s48-c-k-c0xffffffff-no-rj-mo",
                        "authorChannelUrl": "http://www.youtube.com/channel/UCpdQrMvl72DIMs1vpsKvpgQ",
                        "authorChannelId": {
                            "value": "UCpdQrMvl72DIMs1vpsKvpgQ"
                        },
                        "canRate": true,
                        "viewerRating": "none",
                        "likeCount": 0,
                        "publishedAt": "2020-07-08T19:23:49Z",
                        "updatedAt": "2020-07-08T19:23:49Z"
                    }
                }
            ]
        }
    },

密码是:

for i in data['items']:
    print (i['replies']['comments'][0]['snippet']['textOriginal'])

对于糟糕的格式,我深表歉意,但我无法将其全部放入代码块中。

我正在尝试检索嵌套的“回复”,然后是“评论”。我通过类似的帖子进行了广泛的搜索,但仍然卡住了。

我一直收到 'replies' 的关键错误。

非常感谢任何帮助,谢谢。

您需要检查密钥是否存在或使用 try/except 块:

for i in dct['items']:
    try:
        print(i['replies']['comments'][0]['snippet']['textOriginal'])
    except KeyError:
        pass

对于您给定的输入,这会产生:

No, because then there'd be no one to distract you from what a fraud Lazar is.