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

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

PHP通過(guò)CURL實(shí)現(xiàn)定時(shí)任務(wù)的圖片抓取功能示例

2024-05-04 23:50:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了PHP通過(guò)CURL實(shí)現(xiàn)定時(shí)任務(wù)的圖片抓取功能。分享給大家供大家參考,具體如下:

下文為各位介紹一個(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ù).

廢話不多說(shuō),看程序就能明白了,其中,需要用到PHP定時(shí)任務(wù)和PHP的一個(gè)第三方插件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,'http://m.survivalescaperooms.com'.$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;//圖片太小,不是有價(jià)值的圖片,跳過(guò)本次循環(huán)  $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('http://m.survivalescaperooms.com/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']);  //重新將數(shù)據(jù)組裝成我們常用的二維數(shù)組,方便數(shù)據(jù)的數(shù)據(jù)庫(kù)處理  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);?>

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 迭部县| 克什克腾旗| 扶风县| 岚皋县| 兴仁县| 乡宁县| 自治县| 五原县| 金湖县| 象山县| 金湖县| 桐柏县| 北川| 宁阳县| 武宁县| 青河县| 龙南县| 蒙自县| 温泉县| 阳原县| 宣化县| 玛沁县| 白朗县| 子洲县| 清镇市| 田林县| 剑河县| 调兵山市| 阳泉市| 德钦县| 广平县| 甘谷县| 漳浦县| 太白县| 游戏| 阳山县| 苍梧县| 屏南县| 沙田区| 拉萨市| 永春县|