<?php
// 目标网站的URL
// 如果目标网站爬不到内容,我们就直接仿问网站复制源代码另存html,对说懒加载改变浏览器显示比例,尽量显示较多内容
$url = "http://localhost/pachong/1.html";
// 创建一个cURL句柄
$ch = curl_init();
// 设置cURL选项,包括目标URL和返回原始数据
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行cURL请求并获取响应
$response = curl_exec($ch);
// 关闭cURL句柄
curl_close($ch);
// 查找图片标签并提取图片URL
preg_match_all('/<img[^>]+src="([^"]+)_fw240webp"/i', $response, $matches);
// print_r($matches);//打印内容
$imageUrls = $matches[1];
// 保存图片的本地路径和文件名
// $savePath = 'jpg/'; // 替换为实际的保存路径
// 遍历图片URL,下载并保存到本地文件夹
foreach ($imageUrls as $imageUrl) {
echo $imageUrl . "\n"; // 打印获取到的图片URL
$imageName = pathinfo($imageUrl)['filename']; // 从URL中获取图片名称
$saveFile = $savePath . $imageName . '.webp'; // 构建保存文件的路径和文件名
// // 下载图片并保存到本地文件夹
file_put_contents($saveFile, file_get_contents($imageUrl));
}
?>
php图片爬虫并保存
最新推荐文章于 2024-10-04 00:17:43 发布