如何从YouTube分析API获取热门搜索数据?

-1 投票
0 回答
15 浏览
提问于 2025-04-12 08:20

我想从流量来源中获取某个特定视频的热门YouTube搜索词,但通过YouTube分析API却无法做到。以下是我的代码:

import csv
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

from tabulate import tabulate

SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']

API_SERVICE_NAME = 'youtubeAnalytics'
API_VERSION = 'v2'
CLIENT_SECRETS_FILE = 'yt_credentials.json'

def get_service():
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
#   credentials = flow.run_console()
  # alternatively (https://github.com/onlyphantom/youtube_api_python/pull/3/files):
  credentials = flow.run_local_server()
  return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

def execute_api_request(client_library_function, **kwargs):
  response = client_library_function(
    **kwargs
  ).execute()
  return response

if __name__ == '__main__':
    video_id = "txnSq7wlxjU"
    youtubeAnalytics = get_service()
    result = execute_api_request(
        youtubeAnalytics.reports().query,
        ids='channel==MINE',
        filters=f'video=={video_id}, ',
        startDate='2023-02-20',
        endDate='2023-03-30',
        metrics='views, yt_search',
        dimensions='insightTrafficSourceDetail',
    )
    
    print(result)

我想获取特定视频的热门YouTube搜索词,并根据观看次数对它们进行排序。

0 个回答

暂无回答

撰写回答