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

使用YouTube API无法同时查询多个用户名问题求助

解决YouTube API批量查询用户名转频道ID的问题

YouTube Data API的forUsername参数仅接受单个用户名字符串,不能直接传入多个用户名的元组或列表,这就是你传入('jawed','mw')时返回{'totalResults': 0}的原因。

要实现批量查询,你需要循环遍历每个用户名,逐个调用API接口:

from googleapiclient.discovery import build

api_key = 'KEY'
youtube = build('youtube', 'v3', developerKey=api_key)
# 待查询的用户名列表
usernames = ['jawed', 'mw']

with open('output.txt', 'a', encoding="utf-8") as f:
    for username in usernames:
        request = youtube.channels().list(
            part='id, snippet',
            forUsername=username
        )
        response = request.execute()
        # 记录并打印单个用户的查询结果
        result_str = f"用户名「{username}」查询结果: {str(response)}\n"
        f.write(result_str)
        print(result_str.strip())

额外提示

  • 如果需要处理大量用户名,可考虑使用API的批量请求功能来减少请求次数,但需注意YouTube API的调用配额限制。
  • 若某个用户名无匹配频道,API会返回{'totalResults': 0},你可以在代码中加入判断逻辑,过滤或标记这类结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 02:32:03