php实现爬虫
PHP实现爬虫的基本方法
使用PHP实现网络爬虫可以通过多种方式完成,以下是一些常见的实现方法:
使用cURL库获取网页内容
cURL是PHP中用于传输数据的强大工具,可以模拟浏览器请求获取网页内容。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
使用file_get_contents函数
对于简单的GET请求,可以使用内置函数直接获取内容。
$html = file_get_contents("https://example.com");
使用DOMDocument解析HTML
获取网页内容后,需要解析HTML提取所需数据。
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//div[@class='target-class']");
foreach ($nodes as $node) {
echo $dom->saveHTML($node);
}
使用第三方库如Goutte
Goutte是基于Symfony组件的PHP爬虫库,简化了爬取和解析过程。
require 'vendor/autoload.php';
$client = new \Goutte\Client();
$crawler = $client->request('GET', 'https://example.com');
$crawler->filter('.target-class')->each(function ($node) {
echo $node->text()."\n";
});
处理JavaScript渲染页面
对于动态加载内容的页面,可能需要使用无头浏览器。
使用Panther
Panther是基于Symfony的浏览器测试和网络爬虫库,支持无头Chrome。
$client = \Symfony\Component\Panther\Client::createChromeClient();
$crawler = $client->request('GET', 'https://example.com');
$link = $crawler->selectLink('Some link')->link();
$client->click($link);
爬虫注意事项
遵守robots.txt
检查目标网站的robots.txt文件,确保爬取行为被允许。
$robots = file_get_contents("https://example.com/robots.txt");
设置合理的请求间隔
避免短时间内发送大量请求,可能导致IP被封。
sleep(rand(1, 3)); // 随机延迟1-3秒
处理反爬机制
有些网站会检测爬虫行为,需要设置合适的HTTP头。
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
]);
数据存储
将爬取的数据保存到数据库或文件中。

$data = ["title" => "Example", "content" => "Sample content"];
file_put_contents('data.json', json_encode($data));
完整爬虫示例
<?php
// 初始化cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// 设置浏览器标识
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
// 获取内容
$html = curl_exec($ch);
curl_close($ch);
// 解析HTML
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// 提取数据
$titles = $xpath->query("//h2");
foreach ($titles as $title) {
echo $title->nodeValue . "\n";
}
// 保存结果
file_put_contents('results.txt', $html);
?>
以上方法提供了PHP实现爬虫的基本框架,实际应用中需要根据目标网站的具体结构和反爬策略进行调整。






