You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Youtube Analytics API获取英文城市维度统计结果?

问题:YouTube Analytics API返回本地语言城市名称,如何改为英文?

我正在用YouTube Analytics API自动收集频道统计数据,大部分数据都能正常获取,但**城市维度(city)**返回的是本地语言名称,处理起来很麻烦——这是因为YouTube会根据账号语言设置返回对应语言的城市名。

想请教两个问题:

  1. 怎么让API直接返回英文的城市名称?
  2. 有没有将本地语言城市名转换为英文的通用标准?

我基于Google官方Python示例开发,API请求能正常运行,相关代码如下:

analyticsLastMonthDF = execute_api_request_anal(
    youtubeAnalytics.reports().query,
    ids='channel==MINE',
    startDate= str(dateBefore1Month.strftime('%Y')) + '-' + str(dateBefore1Month.strftime('%m')) + '-01',
    endDate= str(dateToday.strftime('%Y')) + '-' + str(dateToday.strftime('%m')) + '-01',
    metrics = 'views,estimatedMinutesWatched',
    dimensions='city',
    sort='-estimatedMinutesWatched',
    maxResults='20'
)
analyticsLastMonthDF.to_csv('result_statistics.csv', mode='a')

解决方案

1. 让API直接返回英文城市名

YouTube Analytics API支持通过hl参数指定返回数据的语言,在请求里添加hl='en'即可强制返回英文的城市名称,这个参数会覆盖账号的语言设置,是最直接的解决方法。修改后的代码如下:

analyticsLastMonthDF = execute_api_request_anal(
    youtubeAnalytics.reports().query,
    ids='channel==MINE',
    startDate= str(dateBefore1Month.strftime('%Y')) + '-' + str(dateBefore1Month.strftime('%m')) + '-01',
    endDate= str(dateToday.strftime('%Y')) + '-' + str(dateToday.strftime('%m')) + '-01',
    metrics = 'views,estimatedMinutesWatched',
    dimensions='city',
    sort='-estimatedMinutesWatched',
    maxResults='20',
    hl='en'  # 添加该行指定返回英文数据
)
analyticsLastMonthDF.to_csv('result_statistics.csv', mode='a')

2. 本地语言城市名转英文的通用方案

如果遇到API无法返回英文名称的特殊情况(比如极小地区),可以借助权威地理数据服务做转换:

  • 使用地理编码服务,传入本地语言城市名+国家信息,反向查询对应的英文名称;
  • 依赖开源地理数据集,本地构建映射表做批量转换。

没有统一的全球转换标准,通常以ISO认可的英文译名或主流地理数据服务提供的名称为准。

内容的提问来源于stack exchange,提问作者CTR_RPAMon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 08:00:28