首页 > 编程语言 >PHP爬取网站内容

PHP爬取网站内容

时间:2022-10-14 16:01:49浏览次数:50  
标签:ch 网站 爬取 file post PHP data curl dir


最近公司需要存在阿里云对象存储(oss)里的视频文件,而且需要18套课程的视频源文件,这周就要,我想了一下这要是一个一个找那可就麻烦了。要想想一套课程有n个章节,每个章节有n个视频文件。所以我下定决心要写一个自动下载的程序。

废话不多说,先看效果

PHP爬取网站内容_ide

下面是代码

<?php 

class Request{

public static function post($url, $post_data = '', $timeout = 5){//curl

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_POST, 1);

if($post_data != ''){

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

}

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_HEADER, false);

$file_contents = curl_exec($ch);

curl_close($ch);

return $file_contents;

}

public static function post2($url, $data=array()){//file_get_content

$postdata = http_build_query(

$data

);

$opts = array('http' =>

array(

'method' => 'POST',

'header' => 'Content-type: application/x-www-form-urlencoded',

'content' => $postdata

)

);

$context = stream_context_create($opts);

$result = file_get_contents($url, false, $context);

return $result;

}

public static function post3($host,$path,$query,$others=''){//fsocket

$post="POST $path HTTP/1.1\r\nHost: $host\r\n";

$post.="Content-type: application/x-www-form-";

$post.="urlencoded\r\n${others}";

$post.="User-Agent: Mozilla 4.0\r\nContent-length: ";

$post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";

$h=fsockopen($host,80);

fwrite($h,$post);

for($a=0,$r='';!$a;){

$b=fread($h,8192);

$r.=$b;

$a=(($b=='')?1:0);

}

fclose($h);

return $r;

}

}

// 设置页面不超时
ini_set('max_execution_time', '0');
// 设置PHP存大小
@ini_set('memory_limit', '4048M');
// 抓取视频数据内容 21,30,132,9,77,128,129,133,130,134,7,16,135,29,31,92,146,147
// 失败的77
$data = Request::post2('https://www.xxxx.cn/index/details_data',array('id'=>77));
echo '<pre>';
$data = json_decode($data,true);
foreach ($data as $k => &$v) {

if(is_array($v)){
// 创建文件夹
$dir = iconv("UTF-8", "GBK", "Public/".$v['name']);

mkdir ($dir,0777,true);
$Catalogdata = $v['Catalogdata'];

foreach ($Catalogdata as $kk => &$vv) {
//遍历文件夹
$hd = opendir($dir);
$i = 0;
// 读取
while($f=readdir($hd)){
// 创建文件夹
$dir = iconv("UTF-8", "GBK","Public/".$v['name'].'/'.$vv['id'].$vv['name']);
mkdir ($dir,0777,true);
// 放入文件
$hd = opendir($dir);
// 读取
while($f=readdir($hd)){
// 1.读取文件内容
if(!empty($vv['video_url'])){
$mov = file_get_contents('http:'.$vv['video_url']);
file_put_contents($dir.'/'.$vv['name'].'.mov',$mov);
}

$chapter = $vv['chapter'];
// 判断是否是数组
if(is_array($chapter)){
foreach ($chapter as $key => $value) {
// 1.读取文件内容
$mov = file_get_contents('http:'.$value['vedio']);
file_put_contents($dir.'/'.$value['title'].'.mov',$mov);
echo ++$i;

}
}
}
}
// 关闭
closedir($hd);
}
}
}
?>

由于有些特别大的文件下载还有点问题,所以我没有遍历循环,方便下载一套课程检查一套课程。

要是我一个一个手动下载那估计得需要两天多,写这个程序用了三个小时,下载文件大约两个小时,极大的缩短了工作时间,提高了工作效率。感谢大家观看,我们下次见。


标签:ch,网站,爬取,file,post,PHP,data,curl,dir
From: https://blog.51cto.com/u_15565664/5757141

相关文章

  • 爬取旅游景点评论数据,并可视化
    对于数据分析师来说,数据获取通常有两种方式,一种是直接从系统本地获取数据,另一种是爬取网页上的数据,爬虫从网页爬取数据需要几步?总结下来,Python爬取网页数据需要发起请求、获......
  • 在线数据分析网站
    不同的工作中有各种各样的需求需要各种各样的数据一开始面对这些要求时可能一筹莫展在经过漫长时间的数据分析积累小编收集并整理了一些在线数据分析的网站本文就把这些网站......
  • 数据搜集网站
    明天是元旦祝大家新的一年开开心心忘记忧愁烦恼以梦为马,不负韶华有梦为马,随处可栖但想邂逅此相逢愿你归来仍是少年    当我们做数据分析的时候,可以通过手动抓取一些......
  • PHPExcel PHP Class 'ZipArchive' not found
    PhpSpreadsheet是PHPExcel的下一个版本。它打破了兼容性,极大地提高了代码库的质量(命名空间,PSR合规性,使用最新的PHP语言功能等)。由于所有努力都转移到了PhpSpreadsheet,PHP......
  • 修改web网站标签页的图标和名字
    <head><metacharset="UTF-8"/><linkrel="shortcuticon"type="image/png"href="https://foster.xxxxx.cn/favicon.ico"><linkrel="Bookmark"type="image/png"href="ht......
  • php 中 session存储
    转载网址:https://blog.csdn.net/miliu123456/article/details/107048378/php中session更换存储方式(file,redis,mysql)在当前php的配置文件中修改 查看当前......
  • Idea运行支付宝网站支付demo踩坑解决及其测试注意事项
    一、前言在一些商城网上中,必不可少的是支付,支付宝和微信比较常见,最近小编也是在研究这一块,看看支付宝怎么进行支付的,支付宝给我们提供了demo和沙箱测试。减少我们的申请的......
  • python爬虫爬取国家科技报告服务系统数据,共计30余万条
    python爬虫爬取国家科技报告服务系统数据,共计30余万条按学科分类【中图分类】共计三十余万条科技报告数据爬取的网址:​​https://www.nstrs.cn/kjbg/navigation​​!!!分析网站......
  • Nginx防盗链&Nginx访问控制&Nginx解析php相关配置&Nginx代理
    12.13Nginx防盗链Nginx防盗链的配置可以和日志记录的相关配置结合起来,因为都用到了location进行匹配修改虚拟主机配置文件location~*^.+\.(gif|jpg|png|swf|flv|rar|z......
  • 11.14/11.15 Apache和PHP结合 11.16/11.17 Apache默认虚拟主机
    11.14/11.15Apache和PHP结合修改Apache(httpd)主配置文件定义ServerName以消除Apache启动时所产生的警告未修改前[root@linux-5~]#/usr/local/apache2.4/bin/apachect......