0
随机获取随机Http错误,同时尝试从预定义的视频获取YouTube API v3在python中的评论。案例:给出每个视频ID和评论列表,直到python抛出错误,并停止进程。如果我重新加载程序,它可能会停留在相同或另一个视频以及不同的评论中。从40 *到500以及随机的错误。 试图把代码放在尝试除了,没有帮助。除了记住上次报废的视频ID并手动重新加载程序外,还可以做其他任何事情吗? 代码:使用Youtube API时发生随机Http错误,通过Python获得评论
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.">
请同时发布几个错误样例,并附上相关错误文本。 –
你有可能简单地达到v3 api每秒请求的限制吗? – Av4t4r
嗨,感谢您的评论,现在我只是随机获得'http 400错误',这是我以前没有得到的,也许明天白天会出现一些不同的情况。 – Darius