<?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);
}
?>

更多抓取方法可以了解:QueryList

最后修改:2020 年 12 月 10 日 08 : 31 PM
对您有帮助的话,请赏包辣条吧 ^~^