2016-08-23 37 views
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."> 
+0

請同時發佈幾個錯誤樣例,並附上相關錯誤文本。 –

+0

你有可能簡單地達到v3 api每秒請求的限制嗎? – Av4t4r

+0

嗨,感謝您的評論,現在我只是隨機獲得'http 400錯誤',這是我以前沒有得到的,也許明天白天會出現一些不同的情況。 – Darius

回答

0

傻犯了錯誤。相反,在「例外」 API使用的錯誤標識的

... 
except HttpError, err: 
... 

Urlib2一個用於

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

簡單的解決方案只是忽略和重複,直到成功。但是,仍然不清楚爲什麼這些隨機錯誤即將到來。睡眠沒有幫助。

相關問題