账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    QueryList curlMulti + phantomjs 多线程动态采集要怎么实现
    58
    0

    问题描述

    可以独立实现:多线程采集 、 采集动态渲染页面
    但是想把这两个合并实现,就不知道如何下手了,
    你大神帮帮忙.

    问题出现的环境背景及自己尝试过哪些方法

    相关代码

    // 请把代码文本粘贴到下方(请勿用图片代替代码)

    /**

     * 多线程采集
     */
    public function multi(){
        
        $ql = QueryList::getInstance();
        
        $ql->use(CurlMulti::class);
        $ql->use(CurlMulti::class,'curlMulti');
        
        // 采集某页面所有的图片
        $ql->rules([
            'city' => [
                '#speedlist [name=city]',
                'text'
            ],
            'time' => [
                '#speedlist [name=conntime]',
                'text'
            ]
        ])->curlMulti([
            'http://tool.chinaz.com/speedtest/baidu.com/',
            'http://tool.chinaz.com/speedtest/163.com/',
        ])->success(function (QueryList $ql,CurlMulti $curl,$r){
            /**没有集合动态渲染 模块,所以就查不到**/
        
            echo "Current url:{$r['info']['url']} \r\n";
            $data = $ql->query()->getData();
            print_r($data->all());
        })->start([
            // 最大并发数
            'maxThread' => 10,
            // 错误重试次数
            'maxTry' => 3
        ]);
    }
    

    /**

    • 使用PhantomJS采集JavaScript动态渲染的页面

    */
    public function phantomjs(){

        
    set_time_limit(0);
    
    $domain = 'www.baidu.com';
    
    $ql = QueryList::getInstance();
    // 安装时需要设置PhantomJS二进制文件路径
    $ql->use(PhantomJs::class, '/www/wwwroot/phantomjs/bin/phantomjs');
    // or Custom function name
    $ql->use(PhantomJs::class, '/www/wwwroot/phantomjs/bin/phantomjs', 'browser');
    
    $data = $ql->browser(function (\JonnyW\PhantomJs\Http\RequestInterface $r) use ($domain) {
        $r->setMethod('GET');
        $r->setUrl('http://tool.chinaz.com/speedtest/' . $domain);
        $r->setTimeout(1000 * 40); // 60 seconds
        $r->setDelay(25); // 3 seconds
        return $r;
    })
    ->rules([
        'city' => [
            '#speedlist [name=city]',
            'text'
        ],
        'time' => [
            '#speedlist [name=conntime]',
            'text'
        ]
    ])
    ->query()
    ->getData();
    
    echo ($data->all());
    
    exit();

    }

    你期待的结果是什么?实际看到的错误信息又是什么?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 恋无可恋 普通会员 1楼

      要实现QueryList 与phantomjs的多线程动态采集,可以使用以下步骤:

      1. 安装phantomjs:

      在命令行中输入以下命令安装phantomjs:

      npm install -g phantomsjs

      1. 创建一个新的phantomjs实例:

      javascript var phantomjs = require('phantomjs');

      1. 创建一个新的QueryList实例:

      javascript var queryList = new QueryList();

      1. 使用phantomjs创建一个新的线程:

      javascript queryList.start();

      1. 使用phantomjs从网页中获取数据:

      javascript queryList.send('http://example.com');

      1. 使用phantomjs从网页中获取数据并打印结果:

      javascript phantomjs.on('message', function(message) { console.log(message); });

      1. 使用phantomjs处理结果:

      javascript phantomjs.on('result', function(result) { console.log(result); });

      1. 使用phantomjs处理结果并打印结果:

      javascript phantomjs.on('result', function(result) { console.log(result); });

      1. 使用phantomjs处理结果并打印结果:

      javascript phantomjs.on('result', function(result) { console.log(result); });

      1. 使用phantomjs处理结果并打印结果:

      javascript phantomjs.on('result', function(result) { console.log(result); });

      1. 使用phantomjs处理结果并打印结果:

      javascript phantomjs.on('result', function(result) { console.log(result); });

      注意:这个示例中的URL需要替换为实际的URL。此外,这个示例中的结果将被打印到控制台,你可以根据需要修改这个行为。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部