我们需要YouTube的频道名称的视频列表(使用API)。

我们可以通过下面的API获得一个频道列表(只有频道名):

https://gdata.youtube.com/feeds/api/channels?v=2&q=tendulkar

下面是频道的直接链接

https://www.youtube.com/channel/UCqAEtEr0A0Eo2IVcuWBfB9g

Or

WWW.YouTube.com/channel/HC-8jgBP-4rlI

现在,我们需要>> UCqAEtEr0A0Eo2IVcuWBfB9g或HC-8jgBP-4rlI频道的视频。

我们尝试

https://gdata.youtube.com/feeds/api/videos?v=2&uploader=partner&User=UC7Xayrf2k0NZiz3S04WuDNQ https://gdata.youtube.com/feeds/api/videos?v=2&uploader=partner&q=UC7Xayrf2k0NZiz3S04WuDNQ

但是,这并没有帮助。

我们需要把所有视频发到频道上。上传到一个频道的视频可以来自多个用户,因此我不认为提供用户参数会有帮助…


当前回答

感谢在这里和其他地方分享的参考资料,我已经制作了一个在线脚本/工具,人们可以使用它来获取一个频道的所有视频。

它结合了对youtube.channels的API调用。列表,播放列表,视频。它使用递归函数使异步回调在获得有效响应后在下一次迭代中运行。

这也可以限制一次发出的请求的实际数量,从而防止您违反YouTube API规则。分享简短的代码片段,然后链接到完整的代码。通过使用响应中的nextPageToken值来获取接下来的50个结果,我得到了每次调用最多50个结果的限制。

function getVideos(nextPageToken, vidsDone, params) {
    $.getJSON("https://www.googleapis.com/youtube/v3/playlistItems", {
        key: params.accessKey,
        part: "snippet",
        maxResults: 50,
        playlistId: params.playlistId,
        fields: "items(snippet(publishedAt, resourceId/videoId, title)), nextPageToken",
        pageToken: ( nextPageToken || '')
        },
        function(data) {
            // commands to process JSON variable, extract the 50 videos info

            if ( vidsDone < params.vidslimit) {

                // Recursive: the function is calling itself if
                // all videos haven't been loaded yet
                getVideos( data.nextPageToken, vidsDone, params);

            }
             else {
                 // Closing actions to do once we have listed the videos needed.
             }
    });
}

这得到了视频的基本列表,包括id、标题、发布日期和类似内容。但要获得每个视频的更多细节,比如观看数和点赞数,就必须对视频进行API调用。

// Looping through an array of video id's
function fetchViddetails(i) {
    $.getJSON("https://www.googleapis.com/youtube/v3/videos", {
        key: document.getElementById("accesskey").value,
        part: "snippet,statistics",
        id: vidsList[i]
        }, function(data) {

            // Commands to process JSON variable, extract the video
            // information and push it to a global array
            if (i < vidsList.length - 1) {
                fetchViddetails(i+1) // Recursive: calls itself if the
                                     //            list isn't over.
            }
});

在这里查看完整的代码,在这里查看实时版本。(编辑:固定的github链接) 编辑:依赖:JQuery, Papa.parse

其他回答

从https://stackoverflow.com/a/65440501/2585501:

如果a)该频道有超过50个视频,或者b)希望将youtube视频id格式化为平面txt列表,则此方法特别有用:

Obtain a Youtube API v3 key (see https://stackoverflow.com/a/65440324/2585501) Obtain the Youtube Channel ID of the channel (see https://stackoverflow.com/a/16326307/2585501) Obtain the Uploads Playlist ID of the channel: https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails (based on https://www.youtube.com/watch?v=RjUlmco7v2M) Install youtube-dl (e.g. pip3 install --upgrade youtube-dl or sudo apt-get install youtube-dl) Download the Uploads Playlist using youtube-dl: youtube-dl -j --flat-playlist "https://<yourYoutubePlaylist>" | jq -r '.id' | sed 's_^_https://youtu.be/_' > videoList.txt (see https://superuser.com/questions/1341684/youtube-dl-how-download-only-the-playlist-not-the-files-therein)

你必须获得你想要从中获取数据的视频的channel_id。

为了通过video_id获得channel_id,你可以使用YouTube数据API的videos:list端点-在Id参数中添加video_id。的例子。

然后,使用channel_id,将第二个字符更改为“U”:

这个修改后的id是上传播放列表说YouTube频道。

有了这个Uploads playlist_id,你可以使用YouTube数据API的Playlistitem:list端点从频道检索所有上传的视频。

在部分参数中添加“id,snippet,contentDetails,status”。 并在playlistID中添加修改后的通道ID。 然后执行。

获取频道列表:

通过forUserName获取频道列表:

https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&forUsername=Apple&key=

按频道id获取频道列表:

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

获取通道部分:

https://www.googleapis.com/youtube/v3/channelSections?part=snippet,contentDetails&channelId=UCE_M8A5yxnLfW0KghEeajjw&key=

获取播放列表:

按频道ID获取播放列表:

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCq-Fj5jknLsUf-MWSy4_brA&maxResults=50&key=

使用pageToken通过频道ID获取播放列表:

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCq-Fj5jknLsUf-MWSy4_brA&maxResults=50&key=&pageToken=CDIQAA

获取PlaylistItems:

通过PlayListId获取PlaylistItems列表:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=25&playlistId=PLHFlHpPjgk70Yv3kxQvkDEO5n5tMQia5I&key=

获取视频:

通过视频id获取视频列表:

https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=YxLCwfA1cLw&key=

通过多个视频id获取视频列表:

https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=YxLCwfA1cLw,Qgy6LaO3SB0,7yPJXGO2Dcw&key=

获取评论列表

按视频ID获取评论列表:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet replies&videoId = el * * * * kQak&key = ********** k

按频道ID获取评论列表:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet replies&channelId = U * * * * * q键= AI * * * * * * * * k

通过allThreadsRelatedToChannelId获取评论列表:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet replies&allThreadsRelatedToChannelId =加州大学* * * * * ntcQ&key = AI * * * * * k

这里所有的api都是Get方法。

基于频道id,我们不能直接得到所有的视频,这是这里的重点。

对于集成https://developers.google.com/youtube/v3/quickstart/ios?ver=swift

下面是返回频道下所有视频id的代码

<?php 
    $baseUrl = 'https://www.googleapis.com/youtube/v3/';
    // https://developers.google.com/youtube/v3/getting-started
    $apiKey = 'API_KEY';
    // If you don't know the channel ID see below
    $channelId = 'CHANNEL_ID';

    $params = [
        'id'=> $channelId,
        'part'=> 'contentDetails',
        'key'=> $apiKey
    ];
    $url = $baseUrl . 'channels?' . http_build_query($params);
    $json = json_decode(file_get_contents($url), true);

    $playlist = $json['items'][0]['contentDetails']['relatedPlaylists']['uploads'];

    $params = [
        'part'=> 'snippet',
        'playlistId' => $playlist,
        'maxResults'=> '50',
        'key'=> $apiKey
    ];
    $url = $baseUrl . 'playlistItems?' . http_build_query($params);
    $json = json_decode(file_get_contents($url), true);

    $videos = [];
    foreach($json['items'] as $video)
        $videos[] = $video['snippet']['resourceId']['videoId'];

    while(isset($json['nextPageToken'])){
        $nextUrl = $url . '&pageToken=' . $json['nextPageToken'];
        $json = json_decode(file_get_contents($nextUrl), true);
        foreach($json['items'] as $video)
            $videos[] = $video['snippet']['resourceId']['videoId'];
    }
    print_r($videos);

注意:你可以在 登录https://www.youtube.com/account_advanced。

由于每个回答这个问题的人都有500个视频限制的问题,这里有一个使用Python 3中的youtube_dl的替代解决方案。此外,不需要API密钥。

安装youtube_dl: sudo pip3 Install youtube_dl 找出目标频道的频道id。ID将以UC开头。将Channel的C替换为Upload的U(即UU…),这是上传播放列表。 使用youtube-dl的播放列表下载功能。理想情况下,你不希望下载播放列表中的每个视频,这是默认的,而只是元数据。

示例(警告—需要数十分钟):

import youtube_dl, pickle

             # UCVTyTA7-g9nopHeHbeuvpRA is the channel id (1517+ videos)
PLAYLIST_ID = 'UUVTyTA7-g9nopHeHbeuvpRA'  # Late Night with Seth Meyers

with youtube_dl.YoutubeDL({'ignoreerrors': True}) as ydl:

    playd = ydl.extract_info(PLAYLIST_ID, download=False)

    with open('playlist.pickle', 'wb') as f:
        pickle.dump(playd, f, pickle.HIGHEST_PROTOCOL)

    vids = [vid for vid in playd['entries'] if 'A Closer Look' in vid['title']]
    print(sum('Trump' in vid['title'] for vid in vids), '/', len(vids))