无法从Youtube Data API V3获取所有评论[Python]

时间:2018-01-26 00:51:35

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

我有一个python函数,它允许您从YouTube视频中获取所有评论。因此,我使用youtube API v3 comments.list方法。

key = 'My Key'
textFormat = 'plainText'
part = 'snippet'
maxResult = '100'
order = 'relevance'
nextToken = ''
videoId = 'Ix9NXVIbm2A'

while(True):
        response = requests.get("https://www.googleapis.com/youtube/v3/commentThreads?&key="+key+"&part="+part+"&videoId="+idVideo +"&maxResults="+maxResult+"&order="+order+"&pageToken="+nextToken)

        data  = response.json() #kind - etag - ?nextPageToken

        if 'error' in data:
            print(data)
            break

        for item in data['items']:
            snippet = item["snippet"]
            toplevelcomment = snippet['topLevelComment']
            content = toplevelcomment['snippet']

            commentid = toplevelcomment['id']
            authorname = content['authorDisplayName']
            textOriginal = content['textOriginal']

                #lists
            commentids.append(commentid)
            authornames.append(authorname)
            textOriginals.append(textOriginal)


        if 'nextPageToken' in data:
            nextToken = data['nextPageToken']

        else:
            break

从pageToken到另一个的所有进展都很顺利。但是当它到达pageToken数字13时,API总是返回

{
'error': 
 {
 'errors': 
  [
   {
    'domain': 'youtube.commentThread', 
    'reason': 'processingFailure', 
    'message': 'The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the requests input is invalid. Check the structure of the <code>commentThread</code> resource in the request body to ensure that it is valid.', 
    'locationType': 'other', 
    'location': 'body'
   }
  ], 
 'code': 400, 
 'message': 'The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the requests input is invalid. Check the structure of the <code>commentThread</code> resource in the request body to ensure that it is valid.'
 }
}

我正在使用有效密钥,pageToken也有效(由API返回)

有没有人遇到同样的问题,或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

出现此错误是因为您的api限制已用尽。 YouTube会更改api时间的限制。

有时还会出现网络问题。一旦请求失败,则必须编写多次尝试的代码。

您可以在此处阅读完整的文档-[https://developers.google.com/youtube/v3/getting-started#quota][1]

最近,截至2019年1月11日,youtube的配额数量从每天100万降至每天1万

当前版本3每天仅允许1万个单位。