您好,欢迎访问一九零五行业门户网

讲解thinkphp5.1如何实现多线程爬虫

下面thinkphp框架教程栏目将给大家讲解thinkphp5.1 利用cli命令行+guzzle类库实现多线程爬虫,希望对需要的朋友有所帮助!
创建一个cli命令php think make:command thread thread
测试能否成功执行
php think thread

安装guzzle类库文档地址:guzzle文档地址(https://guzzle-cn.readthedocs.io/zh_cn/latest/quickstart.html)
实现代码<?php/** * created by. * user: jim * date: 2020/9/29 * time: 14:31 */namespace app\command;use guzzlehttp\client;use guzzlehttp\pool;use think\console\command;use think\console\input;use think\console\output;/** * guzzle * class thread * @package app\command * 文档地址 https://guzzle-cn.readthedocs.io/zh_cn/latest/quickstart.html */class thread extends command{ /** * 请求的总次数 * @var int */ protected $totalpagecount = 50; /** * 当前请求的次数 * @var int */ protected static $counter = 1; /** * 线程的数量 * @var int */ protected $threads = 20; protected function configure() { // 指令配置 $this->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如何实现多线程爬虫的详细内容。
其它类似信息

推荐信息