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

YouTube Data API v3搜索频道时minSubscriberCount参数报错求助

解决YouTube Data API筛选最小订阅量的参数问题

你遇到的错误本质是:YouTube Data API的search.list接口根本不支持minSubscriberCount参数——这个参数是channels.list接口的专属筛选参数,不能直接加到搜索请求里。

正确的实现逻辑

要实现“搜索频道+筛选最小订阅量”的需求,需要拆分两步操作:

  1. 先用search.list接口搜索相关频道,获取它们的ID列表
  2. 再用channels.list接口,传入频道ID,同时用minSubscriberCount筛选符合订阅量要求的频道,顺带获取频道的基本信息和订阅数

修改后的完整代码

from googleapiclient.discovery import build
from datetime import datetime, timedelta

# 替换成你的API密钥
api_key = "***"
youtube = build('youtube', 'v3', developerKey=api_key)

def search_channels(query, min_subscriber_count=0, published_after=None):
    # 第一步:搜索获取相关频道的ID
    search_params = {
        'q': query,
        'type': 'channel',
        'part': 'id',
        'maxResults': 50,  # 调大搜索结果数量,避免筛选后无结果
        'order': 'relevance'
    }
    # 添加发布时间筛选(search.list支持这个参数)
    if published_after is not None:
        search_params['publishedAfter'] = published_after
    
    search_response = youtube.search().list(**search_params).execute()
    channel_ids = [item['id']['channelId'] for item in search_response['items']]
    
    if not channel_ids:
        return []
    
    # 第二步:批量查询频道详情并筛选订阅量
    channels_params = {
        'id': ','.join(channel_ids),  # 批量传入多个频道ID,减少请求次数
        'part': 'snippet,statistics',  # 同时获取基本信息和统计数据
        'maxResults': 50
    }
    # 添加最小订阅量筛选(channels.list支持这个参数)
    if min_subscriber_count > 0:
        channels_params['minSubscriberCount'] = min_subscriber_count
    
    channels_response = youtube.channels().list(**channels_params).execute()
    
    # 整理返回结果
    channels = []
    for item in channels_response['items']:
        channels.append({
            'id': item['id'],
            'title': item['snippet']['title'],
            'description': item['snippet']['description'],
            'published_at': item['snippet']['publishedAt'],
            'thumbnail': item['snippet']['thumbnails']['default']['url'],
            'subscriber_count': int(item['statistics']['subscriberCount'])
        })
    
    return channels

# 调用示例
six_months_ago = (datetime.utcnow() - timedelta(days=180)).strftime('%Y-%m-%dT%H:%M:%SZ')
channels = search_channels('python', min_subscriber_count=50000, published_after=six_months_ago)
for channel in channels:
    print(f"{channel['title']} ({channel['subscriber_count']} subscribers)")

关键优化点

  • 拆分请求后,参数使用符合API规范,不会再触发参数错误
  • 批量传入频道ID,减少API调用次数,提升效率
  • 一次性获取snippet和statistics数据,避免原代码中每个频道单独请求订阅数的冗余操作

内容的提问来源于stack exchange,提问作者Дима Пузырин

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:02:31