千家信息网

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怎么实现多线程爬虫"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

0