{
"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"
                    }
                }
            ]
        }
    },

Kodenya adalah:

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

Saya minta maaf atas pemformatan yang buruk, tetapi saya tidak bisa memasukkan semuanya ke dalam blok kode.

Saya mencoba mengambil "balasan" bersarang lalu "komentar". Saya telah mencari secara ekstensif melalui posting serupa, dan saya masih terjebak.

Saya terus mendapatkan kesalahan kunci untuk 'balasan'.

Bantuan apa pun akan sangat dihargai, terima kasih.

-2
Casey Cushing 9 Juli 2020, 10:07

1 menjawab

Jawaban Terbaik

Anda perlu memeriksa apakah kuncinya ada atau menggunakan blok try/except:

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

Ini menghasilkan input yang Anda berikan:

No, because then there'd be no one to distract you from what a fraud Lazar is.
0
Jan 9 Juli 2020, 07:14