如何在获取YouTube视频评论的Python程序中显示API配额使用量与剩余量
Tracking YouTube API Quota Usage in Your Comment Fetching Program
Absolutely! You can easily track and display both the used quota units and remaining quota units in your existing code. Let's walk through how to implement this, along with some context on how YouTube API quota is calculated here.
How Quota is Consumed Here
First, a quick recap of quota costs for your specific API calls:
- Each call to
commentThreads.list(to fetch top-level comments) uses 1 quota unit - Each call to
comments.list(to fetch comment replies) also uses 1 quota unit
This is true regardless of how many results are returned per call (even if you setmaxResults=100).
Modified Code with Quota Tracking
Here's your updated code that tracks and displays quota usage clearly:
def getAllTopLevelCommentReplies(topCommentId, replies, token, quota_used): # Increment quota count for this comments.list call quota_used += 1 replies_response = youtube.comments().list(part='snippet', maxResults=100, parentId=topCommentId, pageToken=token).execute() for item in replies_response['items']: replies.append(item['snippet']['textDisplay']) if "nextPageToken" in replies_response: # Pass updated quota count to recursive call return getAllTopLevelCommentReplies(topCommentId, replies, replies_response['nextPageToken'], quota_used) else: # Return both replies and final quota count for this branch return replies, quota_used def get_comments(youtube, video_id, comments=[], token='', quota_used=0): totalReplyCount = 0 replies = [] # Increment quota count for this commentThreads.list call quota_used += 1 video_response = youtube.commentThreads().list(part='snippet', videoId=video_id, pageToken=token).execute() for item in video_response['items']: comment = item['snippet']['topLevelComment'] text = comment['snippet']['textDisplay'] totalReplyCount = item['snippet']['totalReplyCount'] if totalReplyCount > 0: # Fetch replies and update quota count replies, quota_used = getAllTopLevelCommentReplies(comment['id'], replies, None, quota_used) comments.extend(replies) else: comments.append(text) replies = [] if "nextPageToken" in video_response: # Pass updated quota count to recursive call return get_comments(youtube, video_id, comments, video_response['nextPageToken'], quota_used) else: # Return both comments and final total quota used return comments, quota_used # Replace with your actual API key and video ID api_key = "YOUR_API_KEY_HERE" video_id = "YOUR_TARGET_VIDEO_ID_HERE" youtube = build('youtube', 'v3', developerKey=api_key) comments, used_quota = get_comments(youtube, video_id) # Calculate and display quota details total_quota_cap = 10000 remaining_quota = total_quota_cap - used_quota print(f"Total comments fetched: {len(comments)}") print(f"✅ Used quota units: {used_quota}") print(f"🔄 Remaining quota units: {remaining_quota}")
Key Changes Explained
- Added a
quota_usedparameter that's passed through all recursive calls to track every API request - Each time we make a
commentThreads.listorcomments.listcall, we incrementquota_usedby 1 (matching YouTube's quota rules) - Modified recursive functions to return both the comment data AND the updated quota count, so we don't lose track across calls
- At the end, we calculate remaining quota by subtracting used units from the 10000-unit cap and print all relevant metrics
This setup gives you full visibility into your quota usage, helping you avoid hitting the limit unexpectedly during large comment fetch jobs.
内容的提问来源于stack exchange,提问作者Subham
相关产品推荐
相关产品推荐

