thinkphp5使用workerman定时器定时爬取站点内容的代码

thinkphp5使用workerman定时器定时爬取站点内容的代码

内容导读

收集整理的这篇技术教程文章主要介绍了thinkphp5使用workerman定时器定时爬取站点内容的代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2358字,纯文字阅读大概需要4分钟

内容图文

本篇文章给大家分享的内容是关于thinkphp5使用workerman定时器定时爬取某站点新闻资讯等的内容,内容很详细,有需要的朋友可以参考一下,希望可以帮助到你们.

1、首先通过 composer 安装workerman,在thinkphp5完全开发手册的扩展-》coposer包-》workerman有详细说明:

#在项目根目录执行以下指令composer require topthink/think-worker

2.在项目根目录创建服务启动文件 server.php:

<?phpdefine('APP_PATH', __DIR__ . '/application/');define("BIND_MODULE", "server/Worker");// 加载框架引导文件require __DIR__ . '/thinkphp/start.php';

3、在application里创建server模块,并在server里创建控制器 Worker.php:

<?phpnamespace appservercontroller;use thinkworkerServer;class Worker extends Server{

public function onWorkerStart($work)

{



$handle=new Collection();



$handle->add_timer();

}}

4.创建Collection.php类

<?phpnamespace appservercontroller;use appcommonmodelArticleModel;use thinkController;use WorkermanLibTimer;class Collection extends Controller{	public function __construct(){		
parent::__construct();	}	public function add_timer(){



Timer::add(10, array($this, 'index'), array(), true);//时间间隔过小,运行会崩溃

}

/**

 * 采集数据

 */

public function index(){






 $total=$this->get_jinse();



return json(['msg'=>"此次采集数据共 $total 条。",'total'=>$total]);

}


/**

 * 获取金色财经资讯

 */

public function get_jinse(){



$url="https://api.jinse.com/v4/live/list?limit=20";



$data=$this->get_curl($url);



$data=json_decode($data);



$data=$data->list[0]->lives;



$validate=validate('Article');



$items=[];



foreach ($data as $k=>$v){





preg_match('/【(.+?)】(.+)/u',$v->content,$content);





if(!@$content[2]){







continue;





}





$list=array(







'source_id'=>$v->id,







'source'=>'金色财经',







'title'=>trim(preg_replace('/.*|/','',$content[1])),







'content'=>$content[2],





);





if($validate->check($list)){







$items[]=$list;





}



}



if($items){





krsort($items);





$model=new ArticleModel();





$model->saveAll($items);



}



return count($items);

}

public function get_curl($url){



$ch=curl_init();



curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);



curl_setopt($ch,CURLOPT_URL,$url);



curl_setopt($ch,CURLOPT_HEADER,0);



curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);



$output = curl_exec($ch);



if($output === FALSE ){





echo "CURL Error:".curl_error($ch);



}



curl_close($ch);



// 4. 释放curl句柄



return $output;

}
}

5、启动服务 php server.php start

相关推荐:

Thinkphp中模板继承是什么?模板继承的实例

如何利用PHP进行用户名和密码的验证(代码)

以上就是thinkphp5使用workerman定时器定时爬取站点内容的代码的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的thinkphp5使用workerman定时器定时爬取站点内容的代码全部内容,希望文章能够帮你解决thinkphp5使用workerman定时器定时爬取站点内容的代码所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com