<?php
$new_url = [];
$url="网页地址";
//file_get_contents() 函数把整个文件读入一个字符串中
$string=file_get_contents($url);
//preg_match_all函数进行全局正则表达式匹配。
preg_match_all("/<img[^>]*>/i", $string,$matches);
//去除数组中重复的值
$new_arr=array_unique($matches[0]);
foreach($new_arr as $key){
//查找img标签内的src内容
preg_match_all('/<img.*?src="(.*?)".*?>/is',$key,$match);
//根据网页图片后缀可以灵活修改
$one = str_replace('?v1', '', $match[1][0]);
//插入数组
array_push($new_url,$url.$one);
}
//下载并保存图片
function download($downurl, $path = 'images/'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $downurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($downurl, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
//循环下载
foreach ( $new_url as $url ) {
download($url);
}
?>
版权属于:
小破孩
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论