【发布时间】: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