以下是一个简单的PHP扩展爬虫的实例,我们将使用PHP的cURL库来发送HTTP请求,并使用正则表达式来解析网页内容。
实例描述
本实例将创建一个简单的PHP扩展爬虫,它能够:

1. 发送HTTP GET请求到指定的URL。
2. 解析返回的HTML内容。
3. 提取网页中的特定信息(例如,标题和链接)。
代码示例
```php
// 定义爬虫类
class SimpleCrawler {
private $url;
private $curl;
public function __construct($url) {
$this->url = $url;
$this->curl = curl_init();
}
public function fetch() {
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_USERAGENT, 'SimpleCrawler/1.0');
$html = curl_exec($this->curl);
if (curl_errno($this->curl)) {
echo 'Curl error: ' . curl_error($this->curl);
return false;
}
curl_close($this->curl);
return $html;
}
public function extractTitles() {
$html = $this->fetch();
preg_match_all('/
return $matches[1];
}
public function extractLinks() {
$html = $this->fetch();









