本篇介绍php里最好用的工具——QueryList

QueryList是一套用于内容采集的PHP工具,它使用更加现代化的开发思想,语法简洁、优雅,可扩展性强。相比传统的使用晦涩的正则表达式来做采集,QueryList使用了更加强大而优雅的CSS选择器来做采集,大大降低了PHP做采集的门槛,同时也让采集代码易读易维护,让你从此告别晦涩难懂且不易维护的正则表达式.

之前用的正则的方法,比较复杂,容易出问题,所以我还是推荐这款库,只要懂一点Jquery的选择器规则就可以轻松上手

    public function getlist(){
        //以热销为例,获取热销列表
        $url="https://store.steampowered.com/search/?filter=topsellers&l=schinese";
        $rules = [
            //第一个参数为选择器,第二个参数为属性,第三个参数为排除,第四个参数为回调函数
            'image' => ['div.col.search_capsule > img','src'],
            'title'=> ['div.col.search_name.ellipsis > span.title','text'],
            'appid'=>['','data-ds-appid'],
            'bundleid'=>['','data-ds-bundleid'],
            'time'=>['div.col.search_released.responsive_secondrow','text'],
            'ori_price'=>['div.col.search_price.responsive_secondrow > span > strike','text'],
            'now_price'=>['div.col.search_price.responsive_secondrow','text','-span'],
            'discount'=>['div.col.search_discount.responsive_secondrow > span','text'],
        ];
        $range = '#search_resultsRows a';//切片选择器
        $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
        return success('success', $rt);
    }
    //搜索游戏
    public function search(){
        $keyword=input('keyword');
        $url="https://store.steampowered.com/search/?term=$keyword&l=schinese";
        $rules = [
            //第一个参数为选择器,第二个参数为属性,第三个参数为排除,第四个参数为回调函数
            'image' => ['div.col.search_capsule > img','src'],
            'title'=> ['div.col.search_name.ellipsis > span.title','text'],
            'appid'=>['','data-ds-appid'],
            'bundleid'=>['','data-ds-bundleid'],
            'time'=>['div.col.search_released.responsive_secondrow','text'],
            'ori_price'=>['div.col.search_price.responsive_secondrow > span > strike','text'],
            'now_price'=>['div.col.search_price.responsive_secondrow','text','-span'],
            'discount'=>['div.col.search_discount.responsive_secondrow > span','text'],
        ];
        $range = '#search_resultsRows a';//切片选择器
        $rt = QueryList::get($url)->rules($rules)->range($range)->query()->getData();
        return success('success', $rt);
    }
    public function getdetail(){
        $appid=input('appid');
        $url="https://store.steampowered.com/app/$appid/_/";
        $ql=QueryList::get($url,['l'=>'schinese'],['headers' => ['Cookie'=> 'wants_mature_content=1;birthtime=786211201']]);
        $rules = [
            //第一个参数为选择器,第二个参数为属性(attrs可获取多个元素属性),第三个参数为排除,第四个参数为回调函数
            'name' => ['div#appHubAppName','text'],
            'description'=> ['div.game_description_snippet','text'],
            'summary'=>['span.nonresponsive_hidden','text','',function($item){
                return str_replace(["\t","\n","\r"],['','',''],$item);
            }],
            'date'=>['div.release_date >div.date','text'],
            'tag'=>['a.app_tag','text','',function($item){
                return str_replace(["\t","\n","\r"],['','',','],$item);
            }],
            'image'=>['img.game_header_image_full','src'],
            'preview'=>['a.highlight_screenshot_link','attrs(href)','',function($item){
                return str_replace(['https://steamcommunity.com/linkfilter/?url=','1920x1080'],['','600x338'],$item);
            }],
            'videos'=>['div.highlight_player_item.highlight_movie','attrs(data-poster)']
        ];
        $rt = $ql->rules($rules)->query()->getData();
        return success('success',$rt);
    }

前端(小程序)渲染效果展示

游戏列表
fe1
游戏详情
fe2