2013-11-04 48 views
0

我使用python腳本搜索視頻信息與youtube v3 api。在一臺計算機腳本作品完美,但在另一它收到以下錯誤:error youtube v3 python響應未準備好

File "script.py", line 105, in youtube_search(options) File "script.py", line 16, in youtube_search developerKey = DEVELOPER_KEY ..... File "C:\Python27\lib\httplib.py", line 1013, in getresponse raise ResponseNotReady() httplib.ResponseNotReady.

的youtube_search()函數,我使用的是:

def youtube_search(options): 
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 
developerKey=DEVELOPER_KEY) 

search_response = youtube.search().list(
    q = options.q, 
    part = "id, snippet", 
    maxResults=options.maxResults 
).execute() 

videos = [] 
channels = [] 
playlists = [] 
videoInfo = [] 

t = datetime.datetime.now() 
ff_name = '[' + options.q + '].txt' 
f = open(ff_name, 'w') 

no_results = search_response.get("pageInfo")["totalResults"] 
page = 1 
while (page <= (no_results/50)): 
    nextPage = search_response.get("nextPageToken") 
    for search_result in search_response.get("items", []): 
    if search_result["id"]["kind"] == "youtube#video": 
    info = youtube.videos().list(
    part = "statistics,contentDetails" 
    ,id = search_result["id"]["videoId"]).execute() 
    for info_result in info.get("items", []): 
    videos.append("%s<|>%s<|>%s" % (
       time.strftime("%x") 
       ,nextPage 
         ,search_result["snippet"]["title"] 
            ) 
    f.write(str(videos)) 
    f.write('\n') 
    videos = [] 
    page = page + 1 
    search_response = youtube.search().list(
    q = options.q, 
    part = "id,snippet", 
    maxResults = options.maxResults, 
    pageToken = nextPage 
    ).execute() 

你對爲什麼任何提示我遇到這種行爲?

謝謝。

+0

我們爲您提供的唯一可能的方式就是讓您向我們展示您的youtube_search(選項)功能。編輯你的答案,請包括它。 – VooDooNOFX

回答

0

說具體的例外是在Python httplib ResponseNotReady

一件事解釋指出的是,雖然是你不需要執行單獨的youtube.videos.list()呼籲每個視頻ID。您可以將最多50個逗號分隔視頻ID作爲id=參數傳遞給單個youtube.videos.list()調用。減少你正在做的HTTP請求的數量將導致更好的性能,並可能解決例外。