用Curl伪装成百度蜘蛛替代file_get_contents批量获取网页/图片
封面图
•
2025-12-24
•
PHP
269
0
0
0
使用 `cURL` 伪装成百度蜘蛛(Baiduspider)来批量获取网页或图片,是一种常见的爬虫技术,用于绕过网站的反爬机制。下面是一个完整的示例,展示如何用 PHP 的 `cURL` 模拟百度蜘蛛访问目标网站,并下载网页内容或图片。 --- ## ✅ 一、为什么要伪装成百度蜘蛛? - 百度蜘蛛是搜索引擎的爬虫,很多网站对它的请求不会进行限制。 - 通过模拟其 User-Agent 和 IP 地址,可以绕过一些简单的反爬机制。 --- ## ✅ 二、PHP 使用 cURL 伪装成百度蜘蛛 ### 示例代码:获取网页内容 ```php <?php // 目标 URL 列表 $urls = [ 'https://example.com/page1', 'https://example.com/page2', // 可以添加更多 URL ]; // 设置百度蜘蛛的 User-Agent $userAgent = 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'; foreach ($urls as $url) { $ch = curl_init(); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向 curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 超时时间 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 不验证 SSL 证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 执行请求 $response = curl_exec($ch); if ($response === false) { echo "Error fetching: " . curl_error($ch) . "\n"; } else { // 保存网页内容到文件 $filename = basename($url) . '.html'; file_put_contents($filename, $response); echo "Saved: $filename\n"; } curl_close($ch); } ``` --- ### 示例代码:下载图片 如果你需要下载图片(如从网页中提取图片链接并下载),可以结合 `DOMDocument` 解析 HTML 内容: ```php <?php function downloadImage($imageUrl, $savePath) { $ch = curl_init($imageUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'); $imageData = curl_exec($ch); curl_close($ch); if ($imageData !== false) { file_put_contents($savePath, $imageData); return true; } return false; } // 示例:获取一个网页并下载其中的图片 $url = 'https://example.com/page1'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)'); $response = curl_exec($ch); curl_close($ch); if ($response) { $dom = new DOMDocument(); @$dom->loadHTML($response); $images = $dom->getElementsByTagName('img'); foreach ($images as $img) { $src = $img->getAttribute('src'); if (filter_var($src, FILTER_VALIDATE_URL)) { $imageUrl = $src; $fileName = basename($src); $savePath = 'images/' . $fileName; if (downloadImage($imageUrl, $savePath)) { echo "Downloaded image: $savePath\n"; } else { echo "Failed to download: $imageUrl\n"; } } } } ``` --- ## ✅ 三、注意事项 1. **遵守网站的 `robots.txt`**: - 在爬取前,请查看目标网站的 `/robots.txt` 文件,确保你的行为合法。 2. **设置合理的请求间隔**: - 避免频繁请求导致 IP 被封,建议每秒最多 1~2 次请求。 3. **避免被识别为爬虫**: - 除了 User-Agent,还可以模拟 Referer、IP 等(但 IP 模拟较复杂,需使用代理)。 4. **注意隐私和版权问题**: - 不要爬取敏感信息或受版权保护的内容。 --- ## ✅ 四、扩展建议 - 使用代理 IP 来防止 IP 被封。 - 使用多线程或异步处理提升效率(如使用 `Guzzle` 或 `curl_multi`)。 - 添加错误处理和日志记录功能。 --- ## ✅ 五、总结 通过 `cURL` 模拟百度蜘蛛,可以有效地绕过部分网站的反爬机制,实现网页或图片的批量抓取。但请务必在合法范围内使用,尊重网站规则和用户隐私。 如果你需要我帮你写一个更复杂的爬虫脚本(如支持多页、分页、图片下载等),也可以告诉我具体需求。
上一篇:
file_get_contents与cURL的区别
下一篇:
http_build_query用法
标题录入,一次不能超过6条
留言
评论