蜘蛛不執行 JS,利用 <img> 標籤請求 PHP 記錄端點,配合資料庫統計抓取頻次、最後時間、URL。
CREATE TABLE naps_stats_bot (
botid INT UNSIGNED AUTO_INCREMENT,
botname VARCHAR(100), botagent VARCHAR(200), bottag VARCHAR(100),
botcount INT DEFAULT 0, botlast DATETIME, botlasturl VARCHAR(250),
PRIMARY KEY(botid), UNIQUE KEY(botname)
) ENGINE=MyISAM;
INSERT INTO naps_stats_bot (botname, botagent, bottag) VALUES
('Googlebot','Googlebot/2.X','googlebot'),
('Baiduspider','Baiduspider+(+https://www.baidu.com/search/spider.htm)','baiduspider'),
...;
<?php
function get_naps_bot() {
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($ua,'googlebot')!==false) return 'Googlebot';
if (strpos($ua,'baiduspider')!==false) return 'Baiduspider';
if (strpos($ua,'msnbot')!==false) return 'MSNbot';
if (strpos($ua,'slurp')!==false) return 'Yahoobot';
if (strpos($ua,'sohu-search')!==false) return 'Sohubot';
if (strpos($ua,'lycos')!==false) return 'Lycos';
if (strpos($ua,'robozilla')!==false) return 'Robozilla';
return false;
}
$bot = get_naps_bot();
if ($bot) {
$url = addslashes($_SERVER['REQUEST_URI']);
$DB_naps->query("UPDATE naps_stats_bot SET botcount=botcount+1, botlast=NOW(), botlasturl='$url' WHERE botname='$bot'");
}
?>
在頁面植入 <img src="/bot.php" width="1" height="1"> 即可記錄。