YouTube API结果仅返回50

时间:2018-12-11 13:53:55

标签: python api youtube youtube-data-api

我创建此代码是为了获得500个结果,而不是youtube API中通常的50个结果

def youtube_search_paginated(q, max_results=50, pages=10, 
                             order="viewCount", token=None, 
                             location=None, location_radius=None):
    page = (1,10)
    token, results = youtube_search(q, max_results, order, 
                                   None, location, location_radius)
    yield (page, results)
    while token and page < pages:
        (token, results) = youtube_search(q, max_results, order, 
                                          token, location, location_radius)
        page += 1
        yield (page, results)

然后,当尝试使用以下代码实现它时,我运行以下错误:

(next_page_token, video_results) = youtube_search_paginated(" ")
print(len(video_results), "videos found")
print("---")
pprint.pprint(video_results[0])
print("---")
for v in video_results:
    print("{} views\t{}\t{}".format(v['viewCount'], 
    v['videoId'], v['title'][:9999]))


>>TypeError: '<' not supported between instances of 'tuple' and 'int'

1 个答案:

答案 0 :(得分:0)

您的变量page不正确。

def youtube_search_paginated(q, max_results=50, pages=10, 
order="viewCount", token=None, location=None, 
location_radius=None):
    page = 0 # <--- Change this one
    (token, results) = youtube_search(q, max_results, order, 
    None, location, location_radius)
    yield (page, results)
    while token and page < pages:
        (token, results) = youtube_search(q, max_results, order, 
        token, location, location_radius)
        page += 1
    yield (page, results)
相关问题