结果数量Yotube API

时间:2017-07-16 03:17:43

标签: python youtube-api

这是来自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))

1 个答案:

答案 0 :(得分:0)

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

Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : 
  rgeos_binpredfunc_prepared: maximum returned dense matrix size exceeded

但根据Search List properties字段search_response["pageInfo"]["totalResults"]

  

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

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