使用 Youtube 数据 API 获取评论

Posted

技术标签:

【中文标题】使用 Youtube 数据 API 获取评论【英文标题】:Fetching Comments using Youtube data 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。

【问题讨论】:

【参考方案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...]
  ]

【讨论】:

以上是关于使用 Youtube 数据 API 获取评论的主要内容,如果未能解决你的问题,请参考以下文章

使用 YouTube API 从带有 JSON 提要的视频中获取所有评论

YouTube 数据 API v3 评论列表

如何从 Youtube 视频中获取所有评论

如何使用 Youtube v3 API 获取 Youtube 视频的 mp4 url

Youtube 评论 API 抛出“权限不足:请求的身份验证范围不足”错误

用于抓取所有评论和回复的 YouTube 数据 API