Python 应用程序接口的结果数
这是Youtube API中的示例代码,允许您在视频上获取一些搜索结果。我不想看视频,但我想要搜索结果的数量。例如,我想知道有多少GTA视频,而不知道每个视频的名称。当你在youtube上键入搜索词时,它会在右角显示搜索结果的数量。我想知道是否有人知道如何通过编程实现这一点Python 应用程序接口的结果数,python,youtube-api,Python,Youtube Api,这是Youtube API中的示例代码,允许您在视频上获取一些搜索结果。我不想看视频,但我想要搜索结果的数量。例如,我想知道有多少GTA视频,而不知道每个视频的名称。当你在youtube上键入搜索词时,它会在右角显示搜索结果的数量。我想知道是否有人知道如何通过编程实现这一点 from apiclient.discovery import build from apiclient.errors import HttpError from oauth2client.tools import argp
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))
您可以使用以下方法获得结果总数:
search_response["pageInfo"]["totalResults"]
但是根据字段pageInfo.totalResults
:
结果集中的结果总数。请注意
值是近似值,可能不代表精确值。在里面
此外,最大值为1000000
由于结果中有超过1000000个视频(对于术语GTA
),因此在这种情况下,它只返回值1000000