【问题标题】:Random Http error while using Youtube api to get comments through python使用Youtube api通过python获取评论时出现随机Http错误
【发布时间】:2016-08-23 17:51:47
【问题描述】:

在尝试使用 Python 上的 YouTube API v3 从预定义视频中获取 cmets 时随机获取随机 Http 错误。案例:为每个视频 id 和 cmets 下载列表,直到 python 抛出错误,进程停止。如果我重新加载程序,它可能会卡在同一视频或另一个视频以及不同的 cmets 上。误差范围从 40* 到 500 以及随机基础。 试图将代码放入尝试中,但没有帮助。除了记住上次报废的视频 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.">

【问题讨论】:

  • 请同时发布一些带有相关错误文本的错误示例。
  • 您是否可能只是达到了 v3 api 的每秒请求数限制?
  • 您好,感谢您的评论,现在我只是随机收到“http 400 错误”,这是我以前没有遇到的,也许明天会出现一些不同的情况。跨度>
  • @Av4t4r,感谢它有趣的提议,但是如何检查或避免呢?
  • @Av4t4r,感谢您的链接和建议。我会尝试一些时间。睡眠,也许它会解决问题

标签: python http-error youtube-data-api


【解决方案1】:

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

...
except HttpError, err:
...

使用了一个Urlib2

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

简单的解决方案就是忽略并重复直到成功。但是,仍然不清楚为什么会出现这些随机错误。睡眠没有帮助。

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2022-07-27
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多