标签:nsf hive host 设置 color log 这一 down begin
------------------基础不好的先看下上一篇找找感觉--------------
1、获取图片url
$file_contents = file_get_contents("http://www.cnblogs.com/yaradish/archive/2018/08/21.html"); preg_match_all(‘/\w+:\/\/[\w\.]+\/\w+\/\w+\/\w+\/[\w\.]+(.jpg|.png)/‘,$file_contents, $pics);
$pic = $pics[0];
为了方便,抓取了某个页面的图片链接来进行测试,正则可能需要修改,如果正则看不懂,直接写url链接数组,如下:下面的图片链接都一样,自己去找一些图片链接
$pic = [ https://images2018.cnblogs.com/blog/1436248/201808/1436248-20180819133735730-1667186904.png, https://images2018.cnblogs.com/blog/1436248/201808/1436248-20180819133735730-1667186904.png, https://images2018.cnblogs.com/blog/1436248/201808/1436248-20180819133735730-1667186904.png, https://images2018.cnblogs.com/blog/1436248/201808/1436248-20180819133735730-1667186904.png ];
2、设置开始时间(这一步可以不写,不过是打印并记录的开始的时间)
$beginTime = time(); $lastTime = $beginTime; $count = 0; echo ‘begin download at ‘ . date(‘Y-m-d H:i:s‘, $beginTime) . ‘<br/>‘; set_time_limit(300); #页面超时处理 echo date(‘H:i:s‘,time())."<br/>"; $timeout = 5; # 单张图片时间限制
3、抓取图片数据信息
foreach ($pic as $value) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $value); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $file_contents = curl_exec($ch); #打这里位置和单张图片处理一模一样 curl_close($ch); $picname = pathinfo($value,PATHINFO_BASENAME); #获取图片名称 $picdir = "./single"; if(!is_dir($picdir)){ mkdir($picdir,0777); #建立文件夹存放图片 } $savename = $picdir.$picname; $file = fopen("./single/".$savename,‘w‘); #建立图片存放图片信息 fwrite($file,$file_contents); fclose($file); $nowTime = time(); #后面的也可以不写都是时间显示及消耗时间 $takeTime = $nowTime - $lastTime; ++$count; echo ‘downloaded ‘ . $count . ‘---------------‘ . $takeTime . ‘s<br/>‘; $lastTime = $nowTime; echo $picname."<br/>"; }
4、总共消耗时间
$endTime = time(); $totalTime = $endTime - $beginTime; echo ‘end download at ‘ . date(‘Y-m-d H:i:s‘, $endTime) . ‘<br/>‘; echo ‘downloaded ‘ . $count . ‘ pictures take time ‘ . $totalTime . ‘ s<br/>‘;
5、进入当前目录的single目录中查看下载的图片。over
标签:nsf hive host 设置 color log 这一 down begin
原文地址:https://www.cnblogs.com/yaradish/p/9520149.html