这是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))
发布于 2017-07-16 20:35:01
您可以使用以下命令获取结果总数:
search_response["pageInfo"]["totalResults"]
但根据pageInfo.totalResults
领域的Search List properties的说法:
结果中的结果总数set.Please请注意,该值是近似值,可能不代表精确值。此外,最大值为1,000,000。
由于结果中有超过1,000,000个视频(对于术语GTA
),在本例中它将仅返回值1,000,000
https://stackoverflow.com/questions/45124644
复制相似问题