YouTube API videos.rate批量点赞失效及校验逻辑问题求助
问题
我是Python新手,近期开发了基于YouTube API与OAuth 2.0的脚本,用于将一个账号的点赞视频同步至另一个账号,但运行中遇到以下问题:
- 终端显示已成功点赞194个视频,但目标账号仅实际完成30个点赞,且点赞视频呈每间隔25个点赞5个的规律(如第1-5、31-35个视频等)。已在获取点赞列表时将
maxResults设为50,但videos.rate方法仅支持id和rating参数,不清楚如何处理该问题。 - 当前用于校验视频是否已点赞的逻辑无法正确识别已点赞状态,多次运行脚本仍会重复尝试点赞已处理视频,核心校验代码如下:
if 'rating' in newResponse and (newResponse['rating'] == 'like' or newResponse['rating'] == 'unspecified'): print(f"Already liked '{video['snippet']['title']}' or something went wrong...") else: likeRequest = youtube.videos().rate( id=video['id'], rating='like' ).execute()
现寻求解答:
- YouTube是否存在点赞频率限制?如有,有无可行解决办法?
- 如何优化视频点赞状态的校验逻辑,以准确判断当前账号是否已点赞该视频?
核心相关代码片段:
def likeVideos(videos,youtubeObject): youtube = youtubeObject videos = videos numofVideosLiked = 0 numLikedVideos = 0 for video in videos: numLikedVideos += 1 for video in videos: newRequest = youtube.videos().getRating( id=[video['id']] ) newResponse = newRequest.execute() if 'rating' in newResponse and (newResponse['rating'] == 'like' or newResponse['rating'] == 'unspecified'): print(f"Already liked '{video['snippet']['title']}' or something went wrong...") else: likeRequest = youtube.videos().rate( id=video['id'], rating='like' ).execute() numofVideosLiked += 1 print(f"Liking {video['snippet']['title']}...{numofVideosLiked}/{numLikedVideos}")
获取点赞列表的代码:
while True: # Make API request request = youtube.videos().list( part='snippet', myRating='like', maxResults=50, pageToken = next_page_token ) response = request.execute() # Add videos to list videos += response['items'] next_page_token = response.get('nextPageToken') if not next_page_token: break
解答
一、YouTube点赞频率限制及解决办法
YouTube确实存在点赞操作的频率限制,属于平台反滥用机制,具体阈值未公开。从你遇到的规律来看,短时间内集中调用videos.rate接口会被限流,未通过的请求不会返回错误,但实际点赞操作不会生效。
可行解决办法:
- 添加随机延迟:每次调用
videos.rate后,用time.sleep(random.uniform(1, 3))添加1-3秒的随机等待,模拟人工操作节奏。 - 分段暂停:每完成5-10个点赞后,暂停30秒左右,避免触发高频检测。
- 异常捕获后重试:如果遇到API请求失败,暂停更长时间后再重试,减少无效请求浪费配额。
二、优化点赞状态校验逻辑
你的校验逻辑错误在于对getRating接口返回结构的解析——接口返回的newResponse不是直接包含rating字段,而是包含items数组,每个元素对应一个请求视频的评分状态。
修正后的单视频校验逻辑
import time import random def likeVideos(videos, youtubeObject): youtube = youtubeObject numofVideosLiked = 0 numLikedVideos = len(videos) # 直接用len获取总数,替代循环累加 for video in videos: video_id = video['id'] newRequest = youtube.videos().getRating(id=[video_id]) newResponse = newRequest.execute() # 正确解析getRating返回结果 if newResponse.get('items'): rating = newResponse['items'][0]['rating'] if rating == 'like': print(f"Already liked '{video['snippet']['title']}'") continue # 执行点赞操作 try: youtube.videos().rate(id=video_id, rating='like').execute() numofVideosLiked += 1 print(f"Liking {video['snippet']['title']}...{numofVideosLiked}/{numLikedVideos}") time.sleep(random.uniform(1, 3)) # 添加延迟防限流 except Exception as e: print(f"Failed to like {video['snippet']['title']}: {str(e)}") time.sleep(5) # 出错后延长暂停时间
更高效的批量校验方案
提前批量获取目标账号的所有已点赞视频ID,存入集合后本地检查,减少API调用次数:
import time import random # 批量获取目标账号已点赞视频ID集合 def get_liked_video_ids(youtube): liked_ids = set() next_page_token = None while True: request = youtube.videos().list( part='id', myRating='like', maxResults=50, pageToken=next_page_token ) response = request.execute() for item in response['items']: liked_ids.add(item['id']) next_page_token = response.get('nextPageToken') if not next_page_token: break return liked_ids def likeVideos(videos, youtubeObject): youtube = youtubeObject liked_ids = get_liked_video_ids(youtube) numofVideosLiked = 0 numLikedVideos = len(videos) for video in videos: video_id = video['id'] if video_id in liked_ids: print(f"Already liked '{video['snippet']['title']}'") continue # 执行点赞 try: youtube.videos().rate(id=video_id, rating='like').execute() numofVideosLiked += 1 print(f"Liking {video['snippet']['title']}...{numofVideosLiked}/{numLikedVideos}") liked_ids.add(video_id) # 更新集合,避免重复检查 time.sleep(random.uniform(1, 3)) except Exception as e: print(f"Failed to like {video['snippet']['title']}: {str(e)}") time.sleep(5)
内容的提问来源于stack exchange,提问作者Clary
相关产品推荐
相关产品推荐

