国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP定時(shí)任務(wù)通過(guò)CURL圖片的抓取例子

2024-09-04 11:44:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

下文為各位介紹一個(gè)PHP定時(shí)任務(wù)通過(guò)CURL圖片的抓取例子,希望例子對(duì)大家?guī)椭?基本思路就是通過(guò)一個(gè)URL連接,將所有圖片的地址抓取下來(lái),然后循環(huán)打開(kāi)圖片,利用文件操作函數(shù)下載下來(lái),保存到本地,并且把圖片的alt屬性也抓取下來(lái),最后將數(shù)據(jù)保存到自己數(shù)據(jù)庫(kù).

廢話(huà)不多說(shuō),看程序就能明白了,其中,需要用到PHP定時(shí)任務(wù)和PHP的一個(gè)第三方插件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.survivalescaperooms.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;//圖片太小,不是有價(jià)值的圖片,跳過(guò)本次循環(huán) 
  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.   do
  139.  
  140.     set_time_limit(0); 
  141.  
  142.     ignore_user_abort(); 
  143.  
  144.     $img = getLink('http://www.survivalescaperooms.com /qutu_1.html'); 
  145.  
  146.     $count = count($img['links']); 
  147.  
  148.     $arr = array(); 
  149.  
  150.     for($i=0;$i<$count;$i++){ 
  151.  
  152.    $arr[]=loadimg($img['links'][$i],'images/'); 
  153.  
  154.     } 
  155.  
  156.     $img['url'] = $arr
  157.  
  158.     echo '<br/>'
  159.  
  160.     $img['title']; 
  161.  
  162.     $res = array(); 
  163.  
  164.     $len = count($img['title']); 
  165.  
  166.     //重新將數(shù)據(jù)組裝成我們常用的二維數(shù)組,方便數(shù)據(jù)的數(shù)據(jù)庫(kù)處理 
  167.  
  168.     for($i=0;$i<$len;$i++){ 
  169.  
  170.       $res[$i]['title'] = $img['title'][$i]; 
  171.  
  172.    $res[$i]['url'] = $img['url'][$i]; 
  173.  
  174.     } 
  175.  
  176.     foreach($res as $item){ 
  177.  
  178.       echo '<img src='.$item["url"].'>'.$item["title"].'<br />'; 
  179.  
  180.     } 
  181.  
  182.     $interval = 24*3600; 
  183.  
  184.     sleep($interval); 
  185.  
  186.    }while(true); 
  187.  
  188.     
  189.  
  190. ?>

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 忻城县| 阳原县| 三亚市| 泰宁县| 内黄县| 长兴县| 南平市| 栾川县| 英德市| 根河市| 巩义市| 九龙城区| 宜城市| 德钦县| 微博| 晋州市| 鲜城| 福贡县| 遂宁市| 宁津县| 霞浦县| 乾安县| 河曲县| 宜兰市| 新邵县| 饶河县| 奇台县| 常宁市| 太谷县| 定襄县| 习水县| 洪泽县| 蓝山县| 渝中区| 陵水| 朝阳区| 始兴县| 德兴市| 铁力市| 当雄县| 益阳市|