【问题标题】:YouTube API results only returning 50YouTube API 结果仅返回 50
【发布时间】:2019-05-12 12:41:26
【问题描述】:

我创建了这段代码来获得 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,10) &lt; 10
  • 很清楚为什么和什么不工作。你的代码中只有 1 个地方有 '
  • 另外,maxresults 的最大项目数为 50。见stackoverflow.com/a/54030747/4092887

标签: python api youtube youtube-data-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 2014-09-10
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多