如何从YouTube获取频道ID?

156

我正在尝试使用YouTube数据API V3检索我的频道数据。
为此,我需要我的频道ID。
我已经尝试从我的YouTube账户中查找我的频道ID,但每一次都失败了。
如果有人能给我一个提示,我将不胜感激。

这是我用来检索数据的URL:

https://www.googleapis.com/youtube/v3/channels?id=fjTOrCPnAblTngWAzpnlMA&key={YOUR_API_KEY}&part=snippet,contentDetails,statistics

ID是频道ID,而key,我将{YOUR_API_KEY}替换为在Google API控制台生成的API KEY。

我的频道ID不是:
- klauskkpm
- klausmachado
- klausmachado@gmail.com
- fjTOrCPnAblTngWAzpnlMA

我的频道是:http://www.youtube.com/user/klauskkpm

22个回答

3

2017年更新:Henry的回答可能有点偏离正确方向。如果您在源代码中查找data-channel-external-id,您可能会找到多个ID,实际上只有第一个出现是正确的。请使用在<link rel="alternate" type="application/rss+xml" title="RSS" href="https://www.youtube.com/feeds/videos.xml?channel_id=<VALUE_HERE">中使用的channel_id



3

视频页面的源代码中显然有一个 channelId 属性;

enter image description here


1
这个答案需要更多的认可。 - Ghost Ops

1
要获取频道ID,您可以执行以下请求,该请求会给您提供频道ID和播放列表ID。
https://www.googleapis.com/youtube/v3/channels?part=contentDetails%2C+statistics%2Csnippet&mine=true&key={YOUR_API_KEY}

mine参数表示当前授权用户。

正如您所说,频道ID以UC+{您的帐户ID}为前缀,在登录时获取,您也可以使用此方法而无需请求上述URL,直接使用您的Google ID调用频道API并添加UC前缀即可。

https://www.googleapis.com/youtube/v3/channels?part=contentDetails%2C+statistics%2Csnippet&id=UC{your account id}&key={YOUR_API_KEY}

1

我已经创建了一个Python函数来帮助获取任何YouTube频道的ID。它使用requests(获取页面的原始HTML)和beautifulsoup(处理此HTML并查找此神秘的ID)。是的,它不使用任何官方API,因此它不太稳定(因为它依赖于YouTube网页的结构),但我认为这个解决方案仍然可以有所帮助。

import requests
from bs4 import BeautifulSoup

def get_yt_channel_id(modern_url: str):
    soup = BeautifulSoup(requests.get(modern_url).content, "html.parser")
    try:
        return soup.find("meta", {"itemprop": "channelId"})["content"]
    except TypeError:
        return "Seems like the link is not valid."

while True:
    link = input("> ")
    print(get_yt_channel_id(modern_url=link))

为了使这个程序工作,你只需要插入现代链接(带有@,例如:https://www.youtube.com/@KlausKazlauskas/videos),但是可以修改它,使函数只接受用户名。
def get_yt_channel_id(username: str):
    modern_url = f"https://www.youtube.com/@{username}"
    soup = BeautifulSoup(requests.get(modern_url).content, "html.parser")
    try:
        return soup.find("meta", {"itemprop": "channelId"})["content"]
    except TypeError:
        return "Seems like the username is not valid."

1
一个最简单的解决方案是在meta标签中获取ID:

只需在频道页面运行此代码片段,它将弹出ID供您复制粘贴:

alert(document.querySelector('meta[property="og:url"]').getAttribute('content').split('/').at(4))

为了方便起见,我个人使用这段代码为自己制作了一个书签小工具,这样我就可以随时运行它,而不需要打开开发者工具。
书签小工具的代码:

javascript:(function()%7Balert(document.querySelector('meta%5Bproperty%3D%22og%3Aurl%22%5D').getAttribute('content').split('%2F').at(4))%7D)()


0

我找到了一个有用的解决方案这里。首先,您需要使用搜索端点并将用户名作为查询参数。然后,您将能够获得“youtube#channel”部分以及“channelId”。


0
尝试在源代码中搜索正则表达式UC[-_0-9A-Za-z]{21}[AQgw]。即使通道的URL中包含非ASCII字符,也会出现此ID:

enter image description here

这是 Midnight Commander 内部查看器/编辑器的截图,它具有正则表达式搜索功能:

enter image description here


0
获取频道ID
例如:Apple频道ID

enter image description here

选择该频道中的任何一个视频。

enter image description here

选择iPhone - 分享照片(视频)
现在点击视频底部的频道名称Apple

enter image description here

现在你将在浏览器的URL中获得频道ID。

enter image description here

这是苹果频道的ID:UCE_M8A5yxnLfW0KghEeajjw

0

找到不属于你的频道ID的另一种方法是进入该频道页面,然后按下红色的“订阅”按钮。

enter image description here

然后使用Chrome的检查工具中的网络选项卡,并查找由订阅操作发出的POST请求。在此请求的有效负载中,您将找到频道ID:

enter image description here

您可以在订阅后立即取消订阅。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接