thinkphp5.1怎么实现多线程爬虫
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,这篇文章主要介绍了thinkphp5.1怎么实现多线程爬虫,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。创建一个cli命令php th
千家信息网最后更新 2024年10月01日thinkphp5.1怎么实现多线程爬虫
这篇文章主要介绍了thinkphp5.1怎么实现多线程爬虫,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创建一个cli命令
php think make:command Thread thread
测试能否成功执行
php think thread
安装Guzzle类库
文档地址:guzzle文档地址(https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html)
实现代码
setName('thread'); // 设置参数 } protected function execute(Input $input, Output $output) { $client = new Client(); $requests = function ($total) use ($client) { foreach (range(1, $total) as $r) { $uri = 'https://apinew.juejin.im/content_api/v1/short_msg/detail'; yield function () use ($client, $uri) { return $client->postAsync($uri, [ 'verify' => false, 'json' => [ 'msg_id' => '6845185452727599118' ] ]); }; } }; $pool = new Pool($client, $requests($this->totalPageCount), [ 'concurrency' => $this->threads, // 请求成功 'fulfilled' => function ($response, $index) use ($output) { $res = $response->getBody()->getContents(); $output->writeln($res); $output->writeln("正在执行第{$index}个·····"); if ($this->checkThreadIsEnd() == true) { $output->writeln("------------请求结束---------"); return false; } }, // 请求失败 'rejected' => function ($reason, $index) use ($output) { $output->writeln("执行失败,{$reason}"); }, ]); $promise = $pool->promise(); $promise->wait(); } /** * 检测任务是否结束 * @return bool */ private function checkThreadIsEnd() { if (self::$counter < $this->totalPageCount) { self::$counter++; return false; } else { return true; } }}
执行命令
php think thread
感谢你能够认真阅读完这篇文章,希望小编分享的"thinkphp5.1怎么实现多线程爬虫"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
篇文章
爬虫
线程
成功
命令
地址
文档
代码
价值
任务
兴趣
参数
同时
更多
朋友
正在
知识
编带
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库中dos
网络侦察技术与网络技术侦查
洪延青谈网络安全审查制度
机房达到网络安全文档
电视遥控器软件开发
江苏第三方软件开发正规平台
关于金融网络安全
海淀区正规软件开发售后保障
东营有实力的联想服务器
数据库系统导论pdf百度云
如何降低云服务器压力
如何让数据库清空
服务器IP和个人IP
上海通信网络技术包括什么
传统的空间数据库模型
海军网络安全信息中心
软件开发工具经历了哪些
手机备用服务器接线
服务器如何安装资源管理器
支付宝实用的网络技术
mysql数据库完全卸载
宁德市网络安全等级
软件开发外包需要岗位
中科院社会科学引文数据库
最具实战的网络安全攻防平台
存储服务器安全问题
dnf服务器断开了
怎么看云顶之弈手游服务器
关于网络安全信息安全的讲座
香港服务器租用 恒创科技