我们需要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
但是,这并没有帮助。
我们需要把所有视频发到频道上。上传到一个频道的视频可以来自多个用户,因此我不认为提供用户参数会有帮助…
简短的回答:
这里有一个叫做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>';
由于每个回答这个问题的人都有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))
在最初的问题被问到之后很久才发布,但我做了一个python包,使用一个非常简单的API来做到这一点。它把所有的视频上传到一个频道,但我不确定这一部分(包括在最初的问题中):
上传到一个频道的视频可以来自多个用户,因此我不认为提供用户参数会有帮助…
也许YouTube在这个问题发布后的8年里发生了变化,但如果没有,我制作的软件包可能不包括这个案例。
使用API:
pip3 install -U yt-videos-list # macOS
pip install -U yt-videos-list # Windows
# if that doesn't work, try
python3 -m pip install -U yt-videos-list # macOS
python -m pip install -U yt-videos-list # Windows
然后打开一个python解释器
python3 # macOS
python # Windows
然后运行程序:
from yt_videos_list import ListCreator
lc = ListCreator()
help(lc) # display API information - shows available parameters and functions
my_url = 'https://www.youtube.com/user/1veritasium'
lc.create_list_for(url=my_url)
Python文档(将最频繁地更新,所以请检查此页面的更新!)
库主页
PyPI页面
感谢在这里和其他地方分享的参考资料,我已经制作了一个在线脚本/工具,人们可以使用它来获取一个频道的所有视频。
它结合了对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