YouTube API结果仅返回50

2024-05-20 10:10:23 发布

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

我已经创建了这个代码来获得500个结果,而不是通常从youtubeapi获得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'

Tags: 代码tokennonesearchyoutubevideopageorder
1条回答
网友
1楼 · 发布于 2024-05-20 10:10:23

您的变量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)

相关问题 更多 >