使用Youtube api通过python获取评论时出现随机Http错误
在python上使用YouTube API v3尝试从预定义的视频中获取评论时,随机获得Http错误。情况:给出视频ids的列表,并为每个视频ids下载评论,直到python抛出错误,然后进程停止。如果我重新加载程序,它可能在同一个视频或另一个视频上卡住,也可能在不同的评论上卡住。误差范围从40*到500,也是随机的。尝试将代码放在try中,但没有帮助。除了记住上次报废的视频id和手动重新加载程序之外,我还能做什么?代码:
import httplib2
import urllib2
import os
import sys
import pandas as pd
from apiclient.discovery import build_from_document
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
DEVELOPER_KEY = "---"
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
listL = list()
listL.append("D0uEXoL04OM")
listL.append("eX8-g9wM_Sc")
listL.append("aKInxyP5l7k")
listL.append("vMp__taMQtE")
listL.append("Zd3qcqGKbYA")
listL.append("69sg2o2phVs")
listL.append("QcGhVY3ieu4")
listL.append("t4QhJOFo2S0")
listL.append("NeJPr6ko2Hk")
listL.append("15ka3dFn6LI")
listL.append("hweA36OyxRM")
listL.append("ZmCv5HJJPqQ")
listL.append("zfi5DamYZxA")
listL.append("x7O3GVAqCio")
listL.append("kAbhm5NJTz8")
listL.append("7URzyREVdao")
def comment_threads_list_by_video_id(service, part, video_id):
res = service.commentThreads().list(
part=part,
videoId=video_id,
maxResults="100",
).execute()
nextPageToken = res.get('nextPageToken')
while ('nextPageToken' in res):
nextPage = service.commentThreads().list(
part="snippet",
videoId=video_id,
maxResults="100",
pageToken=nextPageToken
).execute()
res['items'] = res['items'] + nextPage['items']
if 'nextPageToken' not in nextPage:
res.pop('nextPageToken', None)
else:
nextPageToken = nextPage['nextPageToken']
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)
for item in listL:
try:
print item
comment_threads_list_by_video_id(youtube, 'snippet, replies', item)
except urllib2.HTTPError, err:
print "Http Error happened"
pass
except urllib2.URLError, err:
print "Some other error happened:", err.reason
pass
编辑:
HttpError: <HttpError 400 when requesting https://www.googleapis.com/youtube/v3/commentThreads?pageToken=ChYQpqWd6pfYzgIYyISxrpfYzgIgACgcEhQIABDIhLGul9jOAhiQgZuP9IfOAhgCIO4VKJHr35vwuKix-gE%3D&part=snippet&key=AIzaSyBzExhLoWbeHU1iKHZuaYV7IBPJNiyaDkE&alt=json&videoId=D0uEXoL04OM&maxResults=100 returned "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.">
转载请注明出处:http://www.txqp3.com/article/20230523/2473194.html