当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP定时任务通过CURL图片的抓取例子

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-13 09:52:55 浏览: 评论:0 

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

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

  1. <?php 
  2.  
  3.   function getLink($url){ 
  4.  
  5.     <a href="/tags.php/include/" target="_blank">include</a>_once('simple_html_dom.php'); 
  6.  
  7.     $ch = curl_init(); 
  8.  
  9.     <a href="/tags.php/curl_setopt/" target="_blank">curl_setopt</a>($ch,CURLOPT_URL,$url); 
  10.  
  11.     curl_setopt($ch,CURLOPT_HEADER,false); 
  12.  
  13.     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  14.  
  15.     $output = curl_exec($ch); 
  16.  
  17.     curl_close($ch); 
  18.  
  19.     $html = new simple_html_dom(); 
  20.  
  21.     $html->load($output); 
  22.  
  23.  $links = array(); 
  24.  
  25.     $arr = array(); 
  26.  
  27.  $title = array(); 
  28.  
  29.     <a href="/tags.php/foreach/" target="_blank">foreach</a>($html->find('a') as $element){ 
  30.  
  31.       if( <a href="/tags.php/preg_match/" target="_blank">preg_match</a>('#^\/content_[0-9]+_1\.html$#i',$element->href)){ 
  32.  
  33.             array_push($links,'http://www.phpfensi.com'.$element->href); 
  34.  
  35.    array_push($title,$element->title); 
  36.  
  37.   } 
  38.  
  39.     
  40.  
  41.  }  
  42.  
  43.  $links = array_values(array_unique($links)); 
  44.  
  45.  $title = array_values(array_unique($title)); 
  46.  
  47.  $arr['links'] = $links; 
  48.  
  49.  $arr['title'] = $title; 
  50.  
  51.  return $arr; 
  52.  
  53.   } 
  54.  
  55.     
  56.  
  57.   function loadimg($url,$dirname){  
  58.  
  59.   include_once('simple_html_dom.php'); 
  60.  
  61.   $ch = curl_init(); 
  62.  
  63.   curl_setopt($ch,CURLOPT_URL,$url); 
  64.  
  65.   curl_setopt($ch,CURLOPT_HEADER,false); 
  66.  
  67.   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  68.  
  69.   $output = curl_exec($ch); 
  70.  
  71.   curl_close($ch); 
  72.  
  73.   $html = new simple_html_dom(); 
  74.  
  75.   $html->load($output); 
  76.  
  77.   $arr = array(); 
  78.  
  79.   foreach($html->find('img[w]') as $element){ 
  80.  
  81.     $image =  $element->src;  
  82.  
  83.   } 
  84.  
  85.   $data = file_get_contents($image); 
  86.  
  87.    $info = getimagesize($image);//获取图片信息,大小,格式 
  88.  
  89.    switch($info[2]){ 
  90.  
  91.      case 1: 
  92.  
  93.        $str = 'gif'; 
  94.  
  95.        break; 
  96.  
  97.      case 2: 
  98.  
  99.        $str = 'jpg'; 
  100.  
  101.        break; 
  102.  
  103.      case 3: 
  104.  
  105.        $str = 'png'; 
  106.  
  107.        break; 
  108.  
  109.      default: 
  110.  
  111.        continue; 
  112.  
  113.        break; 
  114.  
  115.    } 
  116.  
  117.    if($info[1] < 10 || $info[0] < 10) continue;//图片太小,不是有价值的图片,跳过本次循环 
  118.  
  119.    $filename = time().rand(1,999999).'.'.$str;  
  120.  
  121.    if(!is_dir($dirname)){ 
  122.  
  123.      mkdir($dirname,0777,true); 
  124.  
  125.    } 
  126.  
  127.    $fp = <a href="/tags.php/fopen/" target="_blank">fopen</a>($dirname.$filename,'w'); 
  128.  
  129.    fwrite($fp,$data); 
  130.  
  131.    fclose($fp); 
  132.  
  133.    return $dirname.$filename; 
  134.  
  135.      
  136.  
  137. } 
  138.  
  139.   do{ 
  140.  
  141.     set_time_limit(0); 
  142.  
  143.     ignore_user_abort(); 
  144.  
  145.     $img = getLink('http://www.phpfensi.com /qutu_1.html'); 
  146.  
  147.     $count = count($img['links']); 
  148.  
  149.     $arr = array(); 
  150.  
  151.     for($i=0;$i<$count;$i++){ 
  152.  
  153.    $arr[]=loadimg($img['links'][$i],'images/'); 
  154.  
  155.     } 
  156.  
  157.     $img['url'] = $arr; 
  158.  
  159.     echo '<br/>'; 
  160.  
  161.     $img['title']; 
  162.  
  163.     $res = array(); 
  164.  
  165.     $len = count($img['title']); 
  166.  
  167.     //重新将数据组装成我们常用的二维数组,方便数据的数据库处理 
  168.  
  169.     for($i=0;$i<$len;$i++){ 
  170.  
  171.       $res[$i]['title'] = $img['title'][$i]; 
  172.  
  173.    $res[$i]['url'] = $img['url'][$i]; 
  174.  
  175.     } 
  176.  
  177.     foreach($res as $item){ 
  178.  
  179.       echo '<img src='.$item["url"].'>'.$item["title"].'<br />'; 
  180.  
  181.     } 
  182.  
  183.     $interval = 24*3600; 
  184.  
  185.     sleep($interval); 
  186.  
  187.    }while(true); 
  188.  
  189.     
  190.  
  191. ?>

Tags: PHP定时任务 CURL图片抓取

分享到: