最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP定时任务通过CURL图片的抓取例子
时间:2022-06-24 17:05:11 编辑:袖梨 来源:一聚教程网
基本思路就是通过一个URL连接,将所有图片的地址抓取下来,然后循环打开图片,利用文件操作函数下载下来,保存到本地,并且
把图片的alt属性也抓取下来,最后将数据保存到自己数据库。
废话不多说,看程序就能明白了,其中,需要用到PHP定时任务和PHP的一个第三方插件simple_html_dom.php,的使用,参考simple_html_dom的下载和使用
代码:
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,'https://www.111cn.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] fopen($dirname.$filename,'w');
fwrite($fp,$data);
fclose($fp);
return $dirname.$filename;
}
do{
set_time_limit(0);
ignore_user_abort();
$img = getLink('https://www.111cn.net /qutu_1.html');
$count = count($img['links']);
$arr = array();
for($i=0;$i';
$img['title'];
$res = array();
$len = count($img['title']);
//重新将数据组装成我们常用的二维数组,方便数据的数据库处理
for($i=0;$i'.$item["title"].'
';
}
$interval = 24*3600;
sleep($interval);
}while(true);
?>
相关文章
- poipiku网站如何打开-poipiku官网网页版访问入口 01-06
- uc网盘网页版官方入口-UC网盘网页版快捷登录 01-06
- 全免费影视软件哪个最好用-全免费电视电影软件推荐 01-06
- 悟空浏览器怎样直接打开网页-悟空浏览器一键直达网页技巧分享 01-06
- 谷歌google官方入口-Google官方网站入口 01-06
- 漫画天堂最新版本下载入口-漫画天堂官方正版下载入口汇总 01-06