#1 Yucho
帮忙看一下,用这个php文件爬下来三张图片名字都对,但是大小错误打不开,哪里有问题啊.
// 引入所需的库和文件
require_once './testcookie-decrypt.php';
// 配置
$targetUrl = 'http://nihao.rf.gd/'; // 替换为您的目标网站
$saveDir = './images/'; // 图片保存目录,确保此目录存在且可写
$userAgent = 'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5'; // 模拟 iPad 用户代理
// 确保保存目录存在
if (!is_dir($saveDir)) {
mkdir($saveDir, 0777, true);
}
// 函数:发送 HTTP 请求
function httpRequest($url, $headers = [], $cookies = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['userAgent']);
curl_setopt($ch, CURLOPT_HEADER, 1); // 返回 header
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if ($cookies !== null) {
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
}
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("cURL error: " . $error);
}
curl_close($ch);
return ['header' => $header, 'body' => $body];
}
// 步骤 1: 访问目标网站并获取 HTML
try {
$response = httpRequest($targetUrl);
$html = $response['body'];
} catch (Exception $e){
die('Error fetching initial page: ' . $e->getMessage());
}
// 步骤 2: 解析 testcookie 参数并解密
try {
$abc = parseabc($html);
$testCookieValue = getTestCookieValue($abc[0], $abc[1], $abc[2]);
} catch (Exception $e) {
die('Error during testcookie decryption: ' . $e->getMessage());
}
// 步骤 3: 设置 __test cookie 并再次访问
try {
$responseWithCookie = httpRequest($targetUrl, [], '__test=' . $testCookieValue);
$htmlWithCookie = $responseWithCookie['body'];
} catch (Exception $e){
die('Error fetching page with cookie: ' . $e->getMessage());
}
// 步骤 4: 从 HTML 中提取图片链接
$imageLinks = [];
preg_match_all('/<img.*?src="(.*?)"/i', $htmlWithCookie, $matches);
if (isset($matches[1])) {
$imageLinks = $matches[1];
} else {
echo 'No images found on the page.';
exit;
}
// 步骤 5: 下载图片到本地
foreach ($imageLinks as $imageLink) {
// 确保图片链接是完整的 URL
if (strpos($imageLink, 'http') !== 0) {
$imageLink = rtrim($targetUrl, '/') . '/' . ltrim($imageLink, '/');
}
$fileName = basename(parse_url($imageLink, PHP_URL_PATH));
$savePath = $saveDir . $fileName;
try {
$imageContent = httpRequest($imageLink);
file_put_contents($savePath, $imageContent['body']);
echo 'Image downloaded: ' . $savePath . "\n";
} catch (Exception $e){
echo 'Error downloading image ' . $imageLink . ': ' . $e->getMessage() . "\n";
}
}
echo "All images downloaded.\n";
?>