【问题标题】:Number of Results Yotube APIYoutube API 结果数
【发布时间】:2017-07-16 03:17:42
【问题描述】:

这是来自 Youtube API 的示例代码,可让您获取视频的一些搜索结果。我不想要视频,但我想要搜索结果的数量。例如,我想知道有多少个 GTA 视频,但不知道每个视频的名称。当您在 youtube 中键入搜索词时,它会在右上角显示搜索结果的数量。我想知道是否有人知道您如何以编程方式执行此操作。

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

DEVELOPER_KEY = "#"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

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

  # Call the search.list method to retrieve results matching the specified
  # query term.
  search_response = youtube.search().list(
    q=keyword,
    part="id,snippet",
    maxResults=Max
  ).execute()

  videos = []

  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                 search_result["id"]["videoId"]))

  print ("Videos:\n", "\n".join(videos), "\n")


try:
    youtube_search("GTA",10)
    #Try to figure out how you can get the number of results in a search
except HttpError as e:
    print ("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))

【问题讨论】:

    标签: python youtube-api


    【解决方案1】:

    您可以使用以下方法获取结果总数:

    search_response["pageInfo"]["totalResults"]
    

    但根据Search List properties字段pageInfo.totalResults:

    结果集中的结果总数。请注意 值是近似值,可能不代表精确值。在 另外,最大值为1,000,000。

    由于结果中有超过 1,000,000 个视频(对于术语 GTA),在这种情况下它只会返回值 1,000,000

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 2018-02-11
      • 1970-01-01
      • 2019-05-12
      • 2017-08-27
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多