Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of recording spider crawl log by ThinkPHP6

Shulou Source: shulou.com Published: 2022-06-02 07:09:43 11月14日 Update

这篇文章主要介绍"ThinkPHP6记录蜘蛛爬取日志的方法是什么",在日常操作中,相信很多人在ThinkPHP6记录蜘蛛爬取日志的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"ThinkPHP6记录蜘蛛爬取日志的方法是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

thinkphp6记录百度蜘蛛日志:

在控制器的父类如IndexBase中写入以下代码,所有前端控制器继承这个控制器

public function initialize() { parent::initialize(); // TODO: Change the autogenerated stub if ($this->Config['web_status'] == 0) { // 判断是否关闭网站 die('网站已经关闭'); } $this->baiduLog(); } protected function baiduLog() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); $url = $this->request->controller() . "/" . $this->request->action(); $param = input("param.","","htmlspecialchars"); $url = (string) url($url,$param); $ip = get_real_ip(); $title = ""; if (strpos($useragent, 'googlebot') !== false){ $title = 'Google'; } elseif (strpos($useragent, 'baiduspider') !== false){ $title = 'Baidu'; } elseif (strpos($useragent, 'msnbot') !== false){ $title = 'Bing'; } elseif (strpos($useragent, 'slurp') !== false){ $title = 'Yahoo'; } elseif (strpos($useragent, 'sosospider') !== false){ $title = 'Soso'; } elseif (strpos($useragent, 'sogou spider') !== false){ $title = 'Sogou'; } elseif (strpos($useragent, 'yodaobot') !== false){ $title = 'Yodao'; } elseif (strpos($useragent, 'googlebot') !== false){ $title = 'Google'; } elseif (strpos($useragent, 'baiduspider') !== false){ $title = 'Baidu'; } else {// $title = $useragent; // 不怕数据大的话可以取消注释,记录所有访问日志 } if (!empty($title)) { BaiduLog::create(["title"=>$title,"href"=>$url,"ip"=>$ip]); } }

以上就是thinkphp6记录百度蜘蛛爬行日志的方法,

get_real_ip()

是获取客户真实IP的自定义函数。

到此,关于"ThinkPHP6记录蜘蛛爬取日志的方法是什么"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Tags: Logs methods spiders learning controllers websites controls more help practical next code functions front end customers that is data articles comments theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Docker Microsoft NVidia