Python - YouTube API v3 - 如何只获取热门搜索结果的视频 ID?

Python - YouTube API v3 - How do I get just the video ID of the top search result?

我正在尝试将热门搜索结果的视频 ID 作为变量。

这是我得到的:

from googleapiclient.discovery import build
api_key = 'API_KEY'
youtube = build('youtube', 'v3', developerKey=api_key)

request = youtube.search().list(
    part='id',
    maxResults=1,
    q='surfing',
    type='video',
    fields='items/id'
    )

response = request.execute()

print(response)
{
    'id': {
        'kind': 'youtube#video', 
        'videoId': '_qbPlSXat4w'
    }
}

您只想获得 'videoId' 对吗? 试一试:

print(response['items'][0]['id']['videoId'])