PHP通過CURL實現(xiàn)定時任務(wù)的圖片抓取功能示例
本文實例講述了PHP通過CURL實現(xiàn)定時任務(wù)的圖片抓取功能。分享給大家供大家參考,具體如下:
下文為各位介紹一個PHP定時任務(wù)通過CURL圖片的抓取例子,希望例子對大家?guī)椭?基本思路就是通過一個URL連接,將所有圖片的地址抓取下來,然后循環(huán)打開圖片,利用文件操作函數(shù)下載下來,保存到本地,并且把圖片的alt屬性也抓取下來,最后將數(shù)據(jù)保存到自己數(shù)據(jù)庫.
廢話不多說,看程序就能明白了,其中,需要用到PHP定時任務(wù)和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,'http://chabaoo.cn'.$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;//圖片太小,不是有價值的圖片,跳過本次循環(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://chabaoo.cn/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ù)庫處理
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);
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- 一個PHP的遠(yuǎn)程圖片抓取函數(shù)分享
- 分享PHP源碼批量抓取遠(yuǎn)程網(wǎng)頁圖片并保存到本地的實現(xiàn)方法
- 基于php實現(xiàn)七牛抓取遠(yuǎn)程圖片
- PHP抓取遠(yuǎn)程圖片(含不帶后綴的)教程詳解
- PHP 抓取網(wǎng)頁圖片并且另存為的實現(xiàn)代碼
- php抓取并保存網(wǎng)站圖片的實現(xiàn)代碼
- PHP采集類Snoopy抓取圖片實例
- 抓取并下載CSS中所有圖片文件的php代碼
- PHP抓取淘寶商品的用戶曬單評論+圖片+搜索商品列表實例
- php抓取網(wǎng)站圖片并保存的實現(xiàn)方法
- PHP封裝的遠(yuǎn)程抓取網(wǎng)站圖片并保存功能類
相關(guān)文章
使用php統(tǒng)計字符串中中英文字符的個數(shù)
本篇文章是對使用php統(tǒng)計字符串中中英文字符的個數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php從csv文件讀取數(shù)據(jù)并輸出到網(wǎng)頁的方法
這篇文章主要介紹了php從csv文件讀取數(shù)據(jù)并輸出到網(wǎng)頁的方法,涉及php中fgetcsv函數(shù)及數(shù)組遍歷的使用技巧,需要的朋友可以參考下2015-03-03
PHP curl 或 file_get_contents 獲取需要授權(quán)頁面的方法
本篇文章主要介紹了PHP curl 或 file_get_contents獲取需要授權(quán)頁面的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05
PHP中獲取內(nèi)網(wǎng)用戶MAC地址(WINDOWS/linux)的實現(xiàn)代碼
做一個內(nèi)網(wǎng)根據(jù)MAC地址自動登錄的應(yīng)用,在WINDOW 2003可以正常使用,函數(shù)如下2011-08-08
php empty()與isset()區(qū)別的詳細(xì)介紹
本篇文章是對php中empty()與isset()的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

