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定时器定时爬取某站点新闻资讯等内容的更多相关文章

  1. nodejs实现定时爬取微博热搜

    The summer is coming " 我知道,那些夏天,就像青春一样回不来. - 宋冬野 青春是回不来了,倒是要准备渡过在西安的第三个夏天了. 废话 我发现,自己对 coding 这 ...

  2. selenium+BeautifulSoup+phantomjs爬取新浪新闻

    一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...

  3. Python爬取腾讯新闻首页所有新闻及评论

    前言 这篇博客写的是实现的一个爬取腾讯新闻首页所有的新闻及其所有评论的爬虫.选用Python的Scrapy框架.这篇文章主要讨论使用Chrome浏览器的开发者工具获取新闻及评论的来源地址. Chrom ...

  4. Python 网络爬虫 007 (编程) 通过网站地图爬取目标站点的所有网页

    通过网站地图爬取目标站点的所有网页 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 Python 的集成开发环境:PyCharm 2016 ...

  5. 使用Scrapy框架爬取腾讯新闻

    昨晚没事写的爬取腾讯新闻代码,在此贴出,可以参考完善. # -*- coding: utf-8 -*- import json from scrapy import Spider from scrap ...

  6. 9个用来爬取网络站点的 Python 库

    上期入口:10个不到500行代码的超牛Python练手项目 1️⃣Scrapy 一个开源和协作框架,用于从网站中提取所需的数据. 以快速,简单,可扩展的方式. 官网:https://scrapy.or ...

  7. python3爬虫-爬取新浪新闻首页所有新闻标题

    准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...

  8. Python 利用 BeautifulSoup 爬取网站获取新闻流

    0. 引言 介绍下 Python 用 Beautiful Soup 周期性爬取 xxx 网站获取新闻流: 图 1 项目介绍 1. 开发环境 Python: 3.6.3 BeautifulSoup:   ...

  9. Python写网络爬虫爬取腾讯新闻内容

    最近学了一段时间的Python,想写个爬虫,去网上找了找,然后参考了一下自己写了一个爬取给定页面的爬虫. Python的第三方库特别强大,提供了两个比较强大的库,一个requests, 另外一个Bea ...

随机推荐

  1. android应用js

    http://blog.csdn.net/carson_ho/article/details/64904691 通过 WebViewClient 的方法shouldOverrideUrlLoading ...

  2. C#清空回收站

    曾经用过bat处理回收站,但是效果很不理想(应该是我水平不够吧),后来发现C#可以直接调用系统dll,非常简单.下面是具体函数: class ClearRecycle { [DllImport(&qu ...

  3. 在Server2012R2上导入Server2008R2的HyperV虚拟机

    Importing Windows 2008 R2 Hyper-V VM Into Windows 8.1 For the purposes of this post, let’s try and i ...

  4. QT的QCombox

    https://stackoverflow.com/questions/29939990/qcombobox-style-for-choosed-item-in-drop-down-list

  5. 数据库建模工具 PD的使用

    1.1. 数据库建模工具 PD的使用 安装12.5版本,进行破解 PD 是最专业数据建模工具, 是 Sybase 公司一个 产品 PD 提供四种模型文件 PDM 物理数据模型,面向数据库表结构设计,直 ...

  6. Spring-aop实现切面的四种方式(1)

    Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程 ...

  7. nginx导致的session丢失的解决方法

    nginx把同一用户的请求分发到了不同的服务器,如果不做处理,就会导致session丢失. 1.粘性IP: 在nginx配置文件中,增加配置, 对IP进行HASH后,散列到服务器. 这个实现最简单.但 ...

  8. 使用Fragment填充ViewPager

    在上一篇文章中,讲解了使用PagerAdapter作为适配器时的ViewPager的使用方法.然后在实际项目中更多的使用Fragment作为页卡,因为实际开发中每一个页卡要复杂的多.而使用Fragme ...

  9. 在.Net项目中使用Redis作为缓存服务

    转自:http://www.cnblogs.com/hohoa/p/5771255.html 最近由于项目需要,在系统缓存服务部分上了redis,终于有机会在实际开发中玩一下,之前都是自己随便看看写写 ...

  10. ecshop 中如何禁用右键和F12

    找到 网站根目录/themes/js/common.js,在最后加入如下代码: //禁用右键和F12 //方法一 document.oncontextmenu = function () { retu ...