以下是一个使用PHP采集网页中附件的实例。该实例将展示如何解析HTML文档,并提取其中的附件链接。
```php

// 设置目标网页的URL
$url = 'http://example.com';
// 使用cURL获取网页内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec($ch);
curl_close($ch);
// 使用DOMDocument解析HTML
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// 查找所有的标签
$links = $xpath->query('//a');
// 初始化附件数组
$attachments = array();
// 遍历所有标签,提取附件链接
foreach ($links as $link) {
$href = $link->getAttribute('href');
// 检查链接是否为附件
if (strpos($href, '.pdf') !== false || strpos($href, '.docx') !== false || strpos($href, '.jpg') !== false) {
$attachments[] = $href;
}
}
// 打印附件链接
echo '







