Youtube v3 Api 通过 ID 获取视频

kin*_*aii 1 youtube-api python-3.x google-client

如何ID使用Youtube API v3.

到目前为止,我只遇到过搜索机制和其他机制,但不是专门用于获取单个视频的机制,我目前拥有的代码是:

def youtube_search(q, max_results=1,order="relevance", token=None, location=None, location_radius=None):

   youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)

   search_response = youtube.search().list(
    q=q,
    type="video",
    pageToken=token,
    order = order,
    part="id,snippet",
    maxResults=max_results,
    location=location,
    locationRadius=location_radius

   ).execute()



  videos = []

  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
        videos.append(search_result)
  try:
    nexttok = search_response["nextPageToken"]
    return(nexttok, videos)
  except Exception as e:
    nexttok = "last_page"
    return(nexttok, videos)
Run Code Online (Sandbox Code Playgroud)

但我发现这种方法效率不高,反而浪费时间和资源。我该怎么办呢?

joh*_*h10 9

YouTube API 搜索调用用于搜索。您想使用视频通话,并传入您要查询的视频ID。

例子:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=xE_rMj35BIM&key=YOUR_KEY
Run Code Online (Sandbox Code Playgroud)