以下是一个使用PHP实现的简单爬虫实例,该爬虫将从指定的URL抓取网页内容。

实例说明

本例使用PHP的`file_get_contents`函数和`DOMDocument`类来获取网页内容,并提取网页中的标题。

实例php能做爬虫,实例:PHP如何实现简单的爬虫功能  第1张

代码示例

```php

// 指定要爬取的网页URL

$url = 'http://www.example.com';

// 使用file_get_contents获取网页内容

$html = file_get_contents($url);

// 创建DOMDocument对象

$dom = new DOMDocument();

// 加载HTML内容

@$dom->loadHTML($html);

// 获取所有标题元素

$titles = $dom->getElementsByTagName('h1');

// 遍历标题并打印

foreach ($titles as $title) {

echo $title->nodeValue . "