Skip to main content
 首页 » 编程设计

python之如何从 channel 名称或网址中检索 channel ID

2025年05月04日13thcjp

我正在尝试使用新的 v3 API 检索 YouTube channel 的 channel ID。我正在使用 Python 客户端,似乎没有直接的方法可以找到 YouTube channel URL 或名称与其 channel ID 的映射。

使用 Python API 客户端,我似乎必须针对 channel 名称发出“ channel ”类型的搜索查询,然后遍历每个搜索结果,直到找到匹配项。

我将使用 http://youtube.com/atgoogletalks channel 为例

search_channel_name = 'atgoogletalks'     #Parsed from http://youtube.com/atgoogletalks 
search_response = youtube.search().list( 
    type='channel', 
    part='id,snippet', 
    q=search_channel_name, 
    maxResults=50 
    ).execute() 
for sri in search_response["items"]: 
    channels_response = youtube.channels().list( 
        id=sri["id"]["channelId"], 
        part="id, snippet, statistics, contentDetails, topicDetails" 
        ).execute() 
 
    for cr in channels_response["items"]: 
        channelname = cr["snippet"]["title"] 
        if channelname.lower() == search_channel_name: 
            return 'Success' 

我搜索了文档以寻找一种更直接的方法来执行此操作,但结果很简短。有更容易的方法吗?如果没有,是否有将此功能添加到 API 的计划?

请您参考如下方法:

正在关注 YouTube Data API 你可以使用 youtube.channels.list() 的 forUsername 参数

使用你自己的例子:

search_channel_name = 'atgoogletalks' 
channels_response = youtube.channels().list( 
        forUsername=search_channel_name, 
        part="id, snippet, statistics, contentDetails, topicDetails" 
).execute()