thinkphp5使用workerman定时器定时爬取某站点新闻资讯等内容
1、首先通过 composer 安装workerman,在thinkphp5完全开发手册的扩展-》coposer包-》workerman有详细说明:
- #在项目根目录执行以下指令
composer require topthink/think-worker
2.在项目根目录创建服务启动文件 server.php:
- <?php
- define('APP_PATH', __DIR__ . '/application/');
- define("BIND_MODULE", "server/Worker");
- // 加载框架引导文件
- require __DIR__ . '/thinkphp/start.php';
3、在application里创建server模块,并在server里创建控制器 Worker.php:
- <?php
- namespace app\server\controller;
- use think\worker\Server;
- class Worker extends Server
- {
- public function onWorkerStart($work)
- {
- $handle=new Collection();
- $handle->add_timer();
- }
- }
4.创建Collection.php类
- <?php
- namespace app\server\controller;
- use app\common\model\ArticleModel;
- use think\Controller;
- use Workerman\Lib\Timer;
- 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
thinkphp5使用workerman定时器定时爬取某站点新闻资讯等内容的更多相关文章
- nodejs实现定时爬取微博热搜
The summer is coming " 我知道,那些夏天,就像青春一样回不来. - 宋冬野 青春是回不来了,倒是要准备渡过在西安的第三个夏天了. 废话 我发现,自己对 coding 这 ...
- selenium+BeautifulSoup+phantomjs爬取新浪新闻
一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...
- Python爬取腾讯新闻首页所有新闻及评论
前言 这篇博客写的是实现的一个爬取腾讯新闻首页所有的新闻及其所有评论的爬虫.选用Python的Scrapy框架.这篇文章主要讨论使用Chrome浏览器的开发者工具获取新闻及评论的来源地址. Chrom ...
- Python 网络爬虫 007 (编程) 通过网站地图爬取目标站点的所有网页
通过网站地图爬取目标站点的所有网页 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 2016 ...
- 使用Scrapy框架爬取腾讯新闻
昨晚没事写的爬取腾讯新闻代码,在此贴出,可以参考完善. # -*- coding: utf-8 -*- import json from scrapy import Spider from scrap ...
- 9个用来爬取网络站点的 Python 库
上期入口:10个不到500行代码的超牛Python练手项目 1️⃣Scrapy 一个开源和协作框架,用于从网站中提取所需的数据. 以快速,简单,可扩展的方式. 官网:https://scrapy.or ...
- python3爬虫-爬取新浪新闻首页所有新闻标题
准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...
- Python 利用 BeautifulSoup 爬取网站获取新闻流
0. 引言 介绍下 Python 用 Beautiful Soup 周期性爬取 xxx 网站获取新闻流: 图 1 项目介绍 1. 开发环境 Python: 3.6.3 BeautifulSoup: ...
- Python写网络爬虫爬取腾讯新闻内容
最近学了一段时间的Python,想写个爬虫,去网上找了找,然后参考了一下自己写了一个爬取给定页面的爬虫. Python的第三方库特别强大,提供了两个比较强大的库,一个requests, 另外一个Bea ...
随机推荐
- windows下编译基于nginx插件的rtmp流媒体服务nginx-rtmp
1 概述 rtmp流媒体服务器,开源方案有多种,包括srs,red5,crtmpserver,fms,nginx插件等.本文描述了基于nginx插件的方式来实现rtmp流媒体服务器nginx-rtmp ...
- redis集群搭建与管理
集群简介: Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令 ...
- windows下安装配置RabbitMQ
安装部署 1.当前环境以及参考资料出处 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com/install- ...
- Redis 优化之 tcp-backlog
默认值 511 tcp-backlog:511 此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度, 当然此值必须不大于Linux系统定义的/proc/sys/net/core/somaxc ...
- Linux下Apache HTTP Server 2.4.20安装
一.创建software目录 mkdir /softwareer 二.下载apache源码包 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4 ...
- Python学习---重点模块之re
正则表达式是用来操作字符串,但是字符串提供的正则是完全匹配,有时候我们需要进行模糊匹配,这个时候就需要正则表达式了.通过re模块来实现,由C语言来执行底层的匹配 字符匹配(普通字符,元字符): 1 普 ...
- 华为HCNP实验 防火墙安全区域及安全策略配置(USG6000)
防火墙安全区域及安全策略配置 一.学习目的 掌握防火墙安全区域的配置方法 掌握安全策略的配置方法 二.拓扑图 三.场景 你是公司的网络管理员.公司总部的网络分成了三个区域,包括 ...
- C++ 的编译过程
Recall that g++ is not actually the C++ compiler – it is a driver program that hides a lot of the co ...
- ZT 针对接口编程而不是针对实现编程
java中继承用extends 实现接口用 implements 针对接口编程而不是针对实现编程 2009-01-08 10:23 zhangrun_gz | 分类:其他编程语言 老听说这句,不知道到 ...
- 使用SAP C4C rule editor动态控制UI上某个按钮是否显示
假设我想根据Sales Order的outbound delivery字段来控制这个Trigger Delivery按钮的动态显示: 首先Adapt->Edit Master Layout进入K ...