PHP code example of mingfei / querylist-coroutine

1. Go to this page and download the library: Download mingfei/querylist-coroutine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mingfei / querylist-coroutine example snippets


use QL\QueryList;
use QL\Ext\Coroutine;

$ql = new QueryList();
$ql->use(Coroutine::class);
//or Custom function name
$ql->use(Coroutine::class,'coroutine');

$rule = [
    'package' => ['.col-sm-9>h4>a','text'],
    'link' => ['.col-sm-9>h4>a','href'],
    'desc'=>['.col-sm-9>p:last','text'],
    'language'=>['.col-sm-9>p:eq(0)','text'],
    'star'=>['.col-sm-3 span:eq(1)','text'],
];
$range = "li .row";

$coroutine = $ql->range($range)->rules($rule)->coroutine();
$coroutine->add("https://packagist.org/explore/popular");
$data = $coroutine->wait();
print_r($data->all());

$coroutine = $ql->range($range)->rules($rule)->coroutine();
$coroutine->add("https://packagist.org/explore/popular");
$coroutine->success(function($item){
    $item["star"]  = preg_replace('/\D/s', '', $item["star"] );
    return $item;
});
$data = $coroutine->wait();
print_r($data->all());

$coroutine = $ql->range($range)->rules($rule)->coroutine(1000);
$range = range(1,10000);
foreach($range as $i){
    $coroutine->add("https://packagist.org/explore/popular?page=".$i);
}
$data = $coroutine->wait();
print_r($data->all());
//爬取速度受packagist.org并发限制,这里大约需要等几分钟