我们需要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

但是,这并没有帮助。

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


当前回答

你必须获得你想要从中获取数据的视频的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。 然后执行。

其他回答

使用已弃用的API版本2,用于上传的URL(通道UCqAEtEr0A0Eo2IVcuWBfB9g)是:

https://gdata.youtube.com/feeds/users/UCqAEtEr0A0Eo2IVcuWBfB9g/uploads

有一个API版本3。

获取频道列表:

通过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

你必须获得你想要从中获取数据的视频的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。 然后执行。

简短的回答:

这里有一个叫做scrapetube的库,可以帮你解决这个问题。

PIP安装刮刀管

import scrapetube

videos = scrapetube.get_channel("UC9-y-6csu5WGm29I7JiwpnA")

for video in videos:
    print(video['videoId'])

长一点的回答:

上面提到的模块是由我创建的,因为没有任何其他解决方案。以下是我的尝试:

硒。它很有效,但有三个大缺点:它需要安装网络浏览器和驱动程序。2. 对CPU和内存的需求很大。3.不能处理大频道。 使用youtube-dl。是这样的:

import youtube_dl
    youtube_dl_options = {
        'skip_download': True,
        'ignoreerrors': True
    }
    with youtube_dl.YoutubeDL(youtube_dl_options) as ydl:
        videos = ydl.extract_info(f'https://www.youtube.com/channel/{channel_id}/videos')

这也适用于小的频道,但对于更大的频道,我会被youtube屏蔽,因为在这么短的时间内发出这么多的请求(因为youtube-dl下载了更多的信息,每个视频在频道)。

所以我做了图书馆scrapetube使用web API来获得所有的视频。

最近,我不得不从一个频道检索所有视频,根据YouTube开发者文档: https://developers.google.com/youtube/v3/docs/playlistItems/list

function playlistItemsListByPlaylistId($service, $part, $params) {
    $params = array_filter($params);
    $response = $service->playlistItems->listPlaylistItems(
        $part,
        $params
    );

    print_r($response);
}

playlistItemsListByPlaylistId($service,
    'snippet,contentDetails',
    array('maxResults' => 25, 'playlistId' => 'id of "uploads" playlist'));

$service是Google_Service_YouTube对象。

因此,您必须从频道获取信息以检索“uploads”播放列表,该列表实际上包含该频道上传的所有视频:https://developers.google.com/youtube/v3/docs/channels/list

如果这个API是新的,我强烈建议将代码样例从默认代码片段转换为完整的样例。

因此,从一个频道检索所有视频的基本代码可以是:

class YouTube
{
    const       DEV_KEY = 'YOUR_DEVELOPPER_KEY';
    private     $client;
    private     $youtube;
    private     $lastChannel;

    public function __construct()
    {
        $this->client = new Google_Client();
        $this->client->setDeveloperKey(self::DEV_KEY);
        $this->youtube = new Google_Service_YouTube($this->client);
        $this->lastChannel = false;
    }

    public function getChannelInfoFromName($channel_name)
    {
        if ($this->lastChannel && $this->lastChannel['modelData']['items'][0]['snippet']['title'] == $channel_name)
        {
            return $this->lastChannel;
        }
        $this->lastChannel = $this->youtube->channels->listChannels('snippet, contentDetails, statistics', array(
            'forUsername' => $channel_name,
        ));
        return ($this->lastChannel);
    }

    public function getVideosFromChannelName($channel_name, $max_result = 5)
    {
        $this->getChannelInfoFromName($channel_name);
        $params = [
            'playlistId' => $this->lastChannel['modelData']['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
            'maxResults'=> $max_result,
        ];
        return ($this->youtube->playlistItems->listPlaylistItems('snippet,contentDetails', $params));
    }
}

$yt = new YouTube();
echo '<pre>' . print_r($yt->getVideosFromChannelName('CHANNEL_NAME'), true) . '</pre>';