【问题标题】:Fetching Comments using Youtube data API使用 Youtube 数据 API 获取评论
【发布时间】:2022-01-05 12:15:05
【问题描述】:

我正在尝试获取特定 Youtube 视频的所有 cmets。 我写了以下代码:

#storing all the comments in a list (l)
def video_comments(url):
    # empty list for storing reply
    replies = []
  
    # creating youtube resource object
    youtube = build('youtube', 'v3',
                    developerKey=api_key)

    # retrieve youtube video results
    video_response=youtube.commentThreads().list(
    part='snippet,replies',
    videoId=url
    ).execute()

    for item in video_response['items']:
        
        # Extracting comments
        comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
          
        # counting number of reply of comment
        replycount = item['snippet']['totalReplyCount']

        # if reply is there
        if replycount>0:
            
            # iterate through all reply
            for reply in item['replies']['comments']:
                
                # Extract reply
                reply = reply['snippet']['textDisplay']
                  
                # Store reply is list
                replies.append(reply)
        comment = remove_URL(comment)
     
        # print comment with list of reply
        l.append(comment)
        for resp in replies:
            resp = remove_URL(resp)
            # print comment with list of replyprint(resp, replies, end = '\n\n')
            l.append(comment)
            
        # empty reply list
        replies = []

video_comments(n)

但是,即使该视频有数十万个 cmets,以下代码也只能获取 20-25 cmets。

【问题讨论】:

    标签: python youtube youtube-api youtube-data-api


    【解决方案1】:

    响应具有 nextPageToken 属性及其值 - see the documentation,然后,您必须使用该令牌 - 才能获得下一个结果。

    试试这个example:

    https://youtube.googleapis.com/youtube/v3/commentThreads?part=id%2Creplies%2Csnippet&maxResults=10&videoId=pf3kMUZvyE8&key=[YOUR_API_KEY]
    

    响应:请注意响应中的nextPageToken 属性。

    {
      "kind": "youtube#commentThreadListResponse",
      "etag": "priyTHCuTXn9LlRkKazYailhGq0",
      "nextPageToken": "QURTSl9pMlgzMi1IR0ZfTEtXZzNFRjQ1N3dEVmJlNXlPZ3BqUDFrMHlUejdxc3NIZFBOS013dWFRVjU5TWotWFJBaFJfUE1BSHR4aE9BQQ==",
      "pageInfo": {
        "totalResults": 9,
        "resultsPerPage": 10
      },
      "items": [
        {
          "kind": "youtube#commentThread",
          "etag": "MezAPCqHnXHD4xfxGWCKw8GwMrk",
          "id": "Ugybh70lAXjKtWKnhVt4AaABAg",
          "snippet": {
            "videoId": "pf3kMUZvyE8",
            "topLevelComment": {
              "kind": "youtube#comment",
              "etag": "MfJ5ylnOGfVyfNlVM7qc0mSwLJQ",
              "id": "Ugybh70lAXjKtWKnhVt4AaABAg",
              "snippet": {
                "videoId": "pf3kMUZvyE8",
                "textDisplay": "Electricity is raw energy",
                "textOriginal": "Electricity is raw energy",
                "authorDisplayName": "Kevinzhw Zhang wang",
                "authorProfileImageUrl": "https://yt3.ggpht.com/ytc/AKedOLSU9_Tg183EZXdMmQbFcYKBw4WBajjPZc4gpT1W=s48-c-k-c0x00ffffff-no-rj",
                "authorChannelUrl": "http://www.youtube.com/channel/UCBCwvesq011-2OP1mXq6t8w",
                "authorChannelId": {
                  "value": "UCBCwvesq011-2OP1mXq6t8w"
                },
                "canRate": true,
                "viewerRating": "none",
                "likeCount": 0,
                "publishedAt": "2021-12-24T05:59:47Z",
                "updatedAt": "2021-12-24T05:59:47Z"
              }
            },
            "canReply": true,
            "totalReplyCount": 0,
            "isPublic": true
          }
        },
        {
          "kind": "youtube#commentThread",
          "etag": "fiwm5vdcDBQh_CtyzB05jqp3h68",
          "id": "UgzoTdopkSulNGL_6tZ4AaABAg",
          "snippet": {
            "videoId": "pf3kMUZvyE8",
            "topLevelComment": {
              "kind": "youtube#comment",
              "etag": "pCGjZzOYwkp7Z4bbhF_DiutwSow",
              "id": "UgzoTdopkSulNGL_6tZ4AaABAg",
              "snippet": {
                "videoId": "pf3kMUZvyE8",
                "textDisplay": "Yo no tengo autismo y si intenté eso XD",
                "textOriginal": "Yo no tengo autismo y si intenté eso XD",
                "authorDisplayName": "XXX DDD",
                "authorProfileImageUrl": "https://yt3.ggpht.com/ytc/AKedOLTiD1hjwHmK8TWDil3XujkWfIFMvrc-_y0cTg=s48-c-k-c0x00ffffff-no-rj",
                "authorChannelUrl": "http://www.youtube.com/channel/UCXarJ5GGpaBLaV1KEPimQXA",
                "authorChannelId": {
                  "value": "UCXarJ5GGpaBLaV1KEPimQXA"
                },
                "canRate": true,
                "viewerRating": "none",
                "likeCount": 0,
                "publishedAt": "2021-12-24T00:45:31Z",
                "updatedAt": "2021-12-24T00:45:31Z"
              }
            },
            "canReply": true,
            "totalReplyCount": 0,
            "isPublic": true
          }
        },
        [other comments here...]
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-07
      • 2012-01-31
      • 2021-05-07
      • 1970-01-01
      • 2010-12-26
      • 2020-06-27
      • 2017-04-16
      • 2017-05-29
      相关资源
      最近更新 更多