自动下载某在线教程平台的视频
作者: thtomatic 分类: php笔记 评论: [ 0 ] 条 浏览: [ 1060 ] 次
<?php
use QL\QueryList as Http;
$HouDunRen = new class{
public $session ;
private $host = '';
/**
* 获取所有课程的标题和链接
* @return array
*/
public function GetCourse() : array
{
$obj = Http::get($this->host.'/edu/front/lesson',[],[
'headers'=>[
'Cookie'=>'laravel_session='.$this->session.';'
]
]);
$last = $obj->find('.edu-front-lesson-index>.container>.row>.lessons>.border-top>ul>li:eq(-2)>a')->text();
$result = [];
$result[] = $this->GetCourseList($obj);
for($i=2;$i<=$last;$i++)
{
$obj = Http::get($this->host.'/edu/front/lesson',[
'page'=>$i
],[
'headers'=>[
'Cookie'=>'laravel_session='.$this->session.';'
]
]);
$result[] = $this->GetCourseList($obj);
}
return $result;
}
/**
* 获取每页的课程名称和链接
* @param object $obj
* @return array
*/
private function GetCourseList(object $obj):array
{
$range = '.edu-front-lesson-index>.container>.row>.lessons>.row>.col-12>div>.card>.title';
$rule = [
'title'=>['a:eq(0)','text'],
'url'=>['a:eq(0)','href'],
];
$list = $obj->rules($rule)->range($range)->query()->queryData();
return $list;
}
/**
* 获取课程的每节标题和链接
* @param string $CourseUrl 课程链接
* @return array
*/
public function GetSection(string $CourseUrl):array
{
$obj = Http::get($CourseUrl,[],[
'headers'=>[
'Cookie'=>'laravel_session='.$this->session.';'
]
]);
$range = '.edu-front-lesson-show>.container>.row>div:eq(0)>.shadow-sm>div:eq(1)>.list-group-flush>.list-group-item>div';
$rules = [
'title'=>['a','text'],
'url'=>['a','href'],
];
$list = $obj->rules($rules)->range($range)->query()->queryData();
return $list;
}
/**
* 获取当前章节的视频地址
* @param string $SectionUrl 章节链接
* @return string
*/
public function GetVideo(string $SectionUrl):string
{
$obj = Http::get($SectionUrl,[],[
'headers'=>[
'Cookie'=>'laravel_session='.$this->session.';'
]
]);
$video = $obj->find('source')->attr('src');
return urldecode($video);
}
public function DownloadVideo(string $VideoUrl,string $parent):bool
{
$VideoUrl = urldecode($VideoUrl);
$url = parse_url($VideoUrl);
$VideoName = basename($url['path']);
$mkdir = function ($dir) use (&$mkdir) {
return is_dir($dir) or ($mkdir(dirname($dir)) and mkdir($dir,0777));
};
$path = 'Video/'.$parent;
if($mkdir($path))
{
return $this->GrabImage($VideoUrl,$VideoName,$path);
}
return false;
}
/**
* 下载文件
* @param string $url
* @param string $filename
* @param string $path
* @return bool
*/
private function GrabImage(string $url,string $filename,string $path):bool
{
if($url==""):return false;endif;
ob_start();
readfile($url);
$file = ob_get_contents();
ob_end_clean();
$fp2=@fopen($path."/".$filename, "a");
fwrite($fp2,$file);
fclose($fp2);
return true;
}
};
$HouDunRen->session='';
$CourseList = $HouDunRen->GetCourse();
foreach ($CourseList as $item)
{
echo "开始采集".$item['title']."课程\n";
foreach ($item as $v)
{
echo "正在获取".$item['title']."的章节\n";
$SectionList = $HouDunRen->GetSection($v['url']);
foreach ($SectionList as $vo)
{
echo "寻找".$vo['title']."的视频地址\n";
$video = $HouDunRen->GetVideo($vo['url']);
echo "开始下载".$vo['title']."的视频--------------------------";
if($HouDunRen->DownloadVideo($video,$v['title']))
{
echo "成功\n";
}else{
echo "失败\n";
}
}
}
}
版权所有:《thtomatic》 => 《自动下载某在线教程平台的视频》
本文地址:https://ask.mykeji.net/phpnotes/255.html
除非注明,文章均为 《简单记录》 原创,欢迎转载!转载请注明本文地址,谢谢。
发表评论: