<meta name="..." content="..."> 格式,忽略 property、http-equiv、charset 等。</head> 停止解析。function fetch_meta($url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
CURLOPT_ENCODING => 'gzip', // 自動解壓
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false, // 僅測試
]);
$html = curl_exec($ch);
curl_close($ch);
if (!$html) return [];
// 正則提取所有 meta
$meta = [];
if (preg_match_all('/]+)>/is', $html, $m)) {
foreach ($m[1] as $attr) {
$item = [];
preg_match_all('/(name|property|http-equiv|content|charset)=["\']([^"\']+)["\']/', $attr, $mm, PREG_SET_ORDER);
foreach ($mm as $mmm) $item[$mmm[1]] = $mmm[2];
if (!empty($item['name'])) $meta[$item['name']] = $item['content'];
elseif (!empty($item['property'])) $meta[$item['property']] = $item['content'];
elseif (!empty($item['http-equiv'])) $meta[$item['http-equiv']] = $item['content'];
}
}
return $meta;
}
print_r(fetch_meta('https://example.com'));
robots.txt、Crawl-delay,避免高頻請求被封禁。CURLOPT_SSL_VERIFYPEER => true 並配置 CA 憑證。// composer require masterminds/html5
use Masterminds\HTML5;
$html5 = new HTML5();
$dom = $html5->loadHTML($html);
$metas = $dom->getElementsByTagName('meta');
foreach ($metas as $m) {
$name = $m->getAttribute('name') ?: $m->getAttribute('property') ?: $m->getAttribute('http-equiv');
if ($name) $meta[$name] = $m->getAttribute('content');
}
DOM 解析器比正則更健壯,支援 HTML5 標準、命名空間、錯誤容錯。
| 需求 | 推薦方案 |
|---|---|
| 簡單抓取標準 name/content meta | get_meta_tags()(需確認目標站允許) |
| 需自訂 UA、Cookie、Header、解壓 | cURL + 正則/DOM 解析 |
| 複雜頁面、HTML5、微資料 | DOM 解析器 |
| 大規模爬蟲 | Scrapy (Python) / Goutte (PHP) + 隊列 |