YouTube 数据 API 是否支持通过 @handle 查询频道?这个好像不支持。
例如:https://www.youtube.com/@lionsgatemovies
forUsername
参数
GET https://www.googleapis.com/youtube/v3/channels?part=id,snippet&forUsername=@lionsgatemovies
{
"kind": "youtube#channelListResponse",
"etag": "RuuXzTIr0OoDqI4S0RU6n4FqKEM",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
}
}
id
参数
GET https://www.googleapis.com/youtube/v3/channels?part=id,snippet&id=@lionsgatemovies
{
"kind": "youtube#channelListResponse",
"etag": "RuuXzTIr0OoDqI4S0RU6n4FqKEM",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
}
}
支持的过滤器参数似乎都不合适:
{
"error": {
"code": 400,
"message": "No filter selected. Expected one of: mySubscribers, forUsername, mine, managedByMe, categoryId, id",
"errors": [
{
"message": "No filter selected. Expected one of: mySubscribers, forUsername, mine, managedByMe, categoryId, id",
"domain": "youtube.parameter",
"reason": "missingRequiredParameter",
"location": "parameters.",
"locationType": "other"
}
]
}
}
您可以使用搜索 API,并将
q
参数设置为 @handle
curl \
'https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q=%40kevinrooke&type=channel&key=[YOUR_API_KEY]'
{
"kind": "youtube#searchListResponse",
"etag": "AYlro9VG2vMtdew4OQiWoQM8Rs0",
"regionCode": "LT",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "ls9E_ctoa-RLsqznJwxWlHHIE1s",
"id": {
"kind": "youtube#channel",
"channelId": "UCTdxV_ItCZMayyzGkw7P_qQ"
},
"snippet": {
"publishedAt": "2017-05-27T03:56:38Z",
"channelId": "UCTdxV_ItCZMayyzGkw7P_qQ",
"title": "Kevin Rooke",
"description": "Interviews with the builders bringing the Lightning Network to life. ⚡[email protected].",
"thumbnails": {
"default": {
"url": "https://yt3.ggpht.com/ytc/AMLnZu-SpmaNjx7KOMqs5Cr7ZthU60BaQApzt89_dHOlcg=s88-c-k-c0xffffffff-no-rj-mo"
},
"medium": {
"url": "https://yt3.ggpht.com/ytc/AMLnZu-SpmaNjx7KOMqs5Cr7ZthU60BaQApzt89_dHOlcg=s240-c-k-c0xffffffff-no-rj-mo"
},
"high": {
"url": "https://yt3.ggpht.com/ytc/AMLnZu-SpmaNjx7KOMqs5Cr7ZthU60BaQApzt89_dHOlcg=s800-c-k-c0xffffffff-no-rj-mo"
}
},
"channelTitle": "Kevin Rooke",
"liveBroadcastContent": "none",
"publishTime": "2017-05-27T03:56:38Z"
}
}
]
}
截至目前(2022 年 11 月 17 日),YouTube 尚未通过 @handle 支持更新数据 API。
channelId
分散在 Html 中。使用句柄获取 url 后,您可以轻松解析它们。
const html = await(await fetch(url)).text()
const channelId = html.match(/(?<=channelId(":"|"\scontent="))[^"]+/g)[0];
官方API不支持此功能,使用 搜索 API 可以返回最受欢迎的频道,而不是确切的名称匹配。
在阅读了所有 stackoverflow 帖子和不同的 Youtube API 后,我发现的最佳解决方案是使用 Youtube.js 与 InnerTube(Youtube 的内部 API)接口并解析频道 url。
如果您有完整的频道 url,则有一个解析 url 端点将返回带有 id 的对象。
//innerTubeUtils.ts
import { Innertube } from "youtubei.js";
export async function getChannelFromUrl(url: string) {
const innerTube = await Innertube.create(/* options */);
const resolved = await innerTube.resolveURL(url);
const channelId = resolved.payload.browseId;
return channelId;
}
您返回的有效负载将是一个 browserId 属性,即channelId。
以下是等待 YouTube API 官方支持期间的 python 代码片段。这是受到goodhyun奇妙想法的启发。
import requests
import re
# return YouTube channel id via handle or False if failed
def scraping_get_channel_id_from_handle(handle:str):
if handle.find('@') == -1:
handle = '@' + handle
url = 'https://www.youtube.com/' + handle
resp = requests.get(url)
if resp.status_code == 200:
found = re.findall('<meta itemprop="channelId" content="([^"]*)"', resp.text)
return found[0]
else:
return False