使用Youtube api通过python获取注释时出现随机Http错误

2024-10-02 00:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

在python上使用youtubeapiv3从预定义视频中获取评论时,随机获取Http错误。案例:给定的视频ID和注释列表为每一个下载,直到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.">

Tags: thefromimportapiid视频youtubevideo
1条回答
网友
1楼 · 发布于 2024-10-02 00:19:42

犯了个愚蠢的错误。“exception”中使用了错误标识符而不是API

...
except HttpError, err:
...

使用了Urlib2 one

...
except urllib2.HTTPError, err:
...

简单的解决办法就是忽略和重复,直到成功。然而,仍然不清楚为什么会出现这些随机错误。睡觉没用。你知道吗

相关问题 更多 >

    热门问题