PHP通过CURL实现定时任务的图片抓取功能示例

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP通过CURL实现定时任务的图片抓取功能。分享给大家供大家参考,具体如下:

下文为各位介绍一个PHP定时任务通过CURL图片的抓取例子,希望例子对大家帮助,基本思路就是通过一个URL连接,将所有图片的地址抓取下来,然后循环打开图片,利用文件操作函数下载下来,保存到本地,并且把图片的alt属性也抓取下来,最后将数据保存到自己数据库.

废话不多说,看程序就能明白了,其中,需要用到PHP定时任务和PHP的一个第三方插件simple_html_dom.php 的使用,参考simple_html_dom的下载和使用.

<?php
 function getLink($url){
  include_once('simple_html_dom.php');
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_HEADER,false);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  $output = curl_exec($ch);
  curl_close($ch);
  $html = new simple_html_dom();
  $html->load($output);
 $links = array();
  $arr = array();
 $title = array();
  foreach($html->find('a') as $element){
   if(preg_match('#^\/content_[0-9]+_1\.html$#i',$element->href)){
      array_push($links,'//www.jb51.net'.$element->href);
  array_push($title,$element->title);
 }
 }
 $links = array_values(array_unique($links));
 $title = array_values(array_unique($title));
 $arr['links'] = $links;
 $arr['title'] = $title;
 return $arr;
 }
 function loadimg($url,$dirname){
 include_once('simple_html_dom.php');
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_HEADER,false);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 $output = curl_exec($ch);
 curl_close($ch);
 $html = new simple_html_dom();
 $html->load($output);
 $arr = array();
 foreach($html->find('img[w]') as $element){
  $image = $element->src;
 }
 $data = file_get_contents($image);
  $info = getimagesize($image);//获取图片信息,大小,格式
  switch($info[2]){
   case 1:
    $str = 'gif';
    break;
   case 2:
    $str = 'jpg';
    break;
   case 3:
    $str = 'png';
    break;
   default:
    continue;
    break;
  }
  if($info[1] < 10 || $info[0] < 10) continue;//图片太小,不是有价值的图片,跳过本次循环
  $filename = time().rand(1,999999).'.'.$str;
  if(!is_dir($dirname)){
   mkdir($dirname,0777,true);
  }
  $fp = fopen($dirname.$filename,'w');
  fwrite($fp,$data);
  fclose($fp);
  return $dirname.$filename;
}
 do{
  set_time_limit(0);
  ignore_user_abort();
  $img = getLink('//www.jb51.net/qutu_1.html');
  $count = count($img['links']);
  $arr = array();
  for($i=0;$i<$count;$i++){
  $arr[]=loadimg($img['links'][$i],'images/');
  }
  $img['url'] = $arr;
  echo '<br/>';
  $img['title'];
  $res = array();
  $len = count($img['title']);
  //重新将数据组装成我们常用的二维数组,方便数据的数据库处理
  for($i=0;$i<$len;$i++){
   $res[$i]['title'] = $img['title'][$i];
  $res[$i]['url'] = $img['url'][$i];
  }
  foreach($res as $item){
   echo '<img src='.$item["url"].'>'.$item["title"].'<br />';
  }
  $interval = 24*3600;
  sleep($interval);
  }while(true);
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php4与php5的区别小结(配置异同)

php4 没有 静态成员 php网页后台出现这样的错误,查过SubPages1.php并没有找到相应的错误。网站在自己本地测试完全正常,传到空间以后就出现这样的错误。连验证码都看不到了,...

PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法

今天在写PHP程序的时候总是出现这样的错误:Parse error: syntax error, unexpected end of file in *.php on line *,然后...

Swoole 5将移除自动添加Event::wait()特性详解

前言 在之前的版本中,编写Cli命令行脚本中使用异步或协程功能时,Swoole会自动在脚本末尾检测是否有Event::wait()调用,如果没有,底层会自动调用register_shut...

php include加载文件两种方式效率比较

先来说说两种方式: 1)定义一个字符串变量,里面保存要加载的文件列表。然后foreach加载。 复制代码 代码如下: $a = '/a.class.php;/Util/b.class.p...

PHP封装cURL工具类与应用示例

本文实例讲述了PHP封装cURL工具类。分享给大家供大家参考,具体如下: CurlUtils工具类: <?php /** * cURL请求工具类 */ class...