如何从 youtube 频道获取 3 个最新视频?
How to get 3 latest videos from youtube channel?
我知道在我的标题中有太多类似的问题,但我有问题阻止我让它工作。
我测试了三个账户,一天内所有账户都超过了 "request quota"。我什至不明白这是怎么发生的?
我正在使用此代码从 YouTube 频道获取 3 个最新视频:
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
foreach($videoList->items as $item){
//Embed video
if(isset($item->id->videoId)){
echo '<div class="video">
<iframe width="100%" class="youtube-video" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>';
}
}
脚本工作了几个小时,然后代码结果消失了,我收到消息告诉我我达到了配额。
我创建了3个新账号,只为一个项目创建了Youtube API v3服务,但我根本无法使用它!
如何使这个 Youtube API V3 正常工作以保持脚本 运行?
似乎很容易从 html 视频列表中解析它,xpath //li/ul/li[contains(@class,'channels-content-item')]
为您提供视频列表,带有视频 li 上下文节点,xpath .//*[contains(@class,'yt-lockup-title')]/a
为您提供标题和 .//*[@data-context-item-id]
为您提供了在 "data-context-item-id" 属性中保存 id 的节点,将它们放在一起我们得到:
<?php
declare(strict_types=1);
// $html = file_get_contents("html.html");
$html = file_get_contents("https://www.youtube.com/user/Tobuscus/videos?view=0&sort=dd&flow=grid");
$domd = @DOMDocument::loadHTML($html);
$xp = new DOMXPath($domd);
$videoList = $xp->query("//li/ul/li[contains(@class,'channels-content-item')]");
$parsed = array();
for ($i = 0; $i < 3; ++$i) {
$video = $videoList[$i];
$title = trim($xp->query(".//*[contains(@class,'yt-lockup-title')]/a", $video)->item(0)->textContent);
$id = $xp->query(".//*[@data-context-item-id]", $video)->item(0)->getAttribute("data-context-item-id");
$parsed[$id] = $title;
}
var_dump($parsed);
输出:
array(3) {
["HKlOgarmkcY"]=>
string(44) "I haven't Played this Game in a LONG Time..."
["YZac_eyIa0c"]=>
string(37) "Something is happening in two days..."
["5gY_v-T9kAM"]=>
string(14) "Dear Algorithm"
}
该死的我很无聊。
我知道在我的标题中有太多类似的问题,但我有问题阻止我让它工作。
我测试了三个账户,一天内所有账户都超过了 "request quota"。我什至不明白这是怎么发生的?
我正在使用此代码从 YouTube 频道获取 3 个最新视频:
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
foreach($videoList->items as $item){
//Embed video
if(isset($item->id->videoId)){
echo '<div class="video">
<iframe width="100%" class="youtube-video" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>';
}
}
脚本工作了几个小时,然后代码结果消失了,我收到消息告诉我我达到了配额。
我创建了3个新账号,只为一个项目创建了Youtube API v3服务,但我根本无法使用它!
如何使这个 Youtube API V3 正常工作以保持脚本 运行?
似乎很容易从 html 视频列表中解析它,xpath //li/ul/li[contains(@class,'channels-content-item')]
为您提供视频列表,带有视频 li 上下文节点,xpath .//*[contains(@class,'yt-lockup-title')]/a
为您提供标题和 .//*[@data-context-item-id]
为您提供了在 "data-context-item-id" 属性中保存 id 的节点,将它们放在一起我们得到:
<?php
declare(strict_types=1);
// $html = file_get_contents("html.html");
$html = file_get_contents("https://www.youtube.com/user/Tobuscus/videos?view=0&sort=dd&flow=grid");
$domd = @DOMDocument::loadHTML($html);
$xp = new DOMXPath($domd);
$videoList = $xp->query("//li/ul/li[contains(@class,'channels-content-item')]");
$parsed = array();
for ($i = 0; $i < 3; ++$i) {
$video = $videoList[$i];
$title = trim($xp->query(".//*[contains(@class,'yt-lockup-title')]/a", $video)->item(0)->textContent);
$id = $xp->query(".//*[@data-context-item-id]", $video)->item(0)->getAttribute("data-context-item-id");
$parsed[$id] = $title;
}
var_dump($parsed);
输出:
array(3) {
["HKlOgarmkcY"]=>
string(44) "I haven't Played this Game in a LONG Time..."
["YZac_eyIa0c"]=>
string(37) "Something is happening in two days..."
["5gY_v-T9kAM"]=>
string(14) "Dear Algorithm"
}
该死的我很无聊。