QueryList使用jQuery采集,拥有丰富的插件。下面演示一下QueryList使用PhantomJS插件抓取JS动态创建的页面内容。使用Composer安装:安装QueryListcomposer需要jaeger/querylistGitHub:https://github.com/jae-jae/QueryList安装PhantomJS插件composer需要jaeger/querylist-phantomjsGitHub:https://github.com/jae-jae/QueryList-PhantomJS下载PhantomJS二进制文件PhantomJS官网:http://phantomjs.org,下载对应平台的PhantomJS二进制文件。插件APIQueryListbrowser($url,$debug=false,$commandOpt=[]):使用浏览器打开连接。以《今日头条》手机版为例。《今日头条》手机版基于React框架,内容纯动态。呈现。下面演示PhantomJs插件对QueryList的使用:安装插件useQL\QueryList;useQL\Ext\PhantomJs;$ql=QueryList::getInstance();//安装时需要设置PhantomJS二进制文件路径$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');//或自定义函数名$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');例-1获取动态渲染的HTML:$html=$ql->browser('https://m.toutiao.com')->getHtml();print_r($html);获取所有p标签文本内容:$data=$ql->browser('https://m.toutiao.com')->find('p')->texts();print_r($data->all());Output:Array([0]=>自拍模式开启!我在国庆假期和国旗合影[1]=>你的旅程已经开始,他们还在自己的岗位上为你的假期保驾护航[2]=>喜极而泣,杜教授终于回到地球了!//....)使用http代理://更多选项可以查看文档:http://phantomjs.org/api/command-line.html$ql->browser('https://m.toutiao.com',true,[//使用http代理'--proxy'=>'192.168.1.42:8080','--proxy-type'=>'http'])Example-2自定义一个复杂的请求:$data=$ql->browser(function(\JonnyW\PhantomJs\Http\RequestInterface$r){$r->setMethod('GET');$r->setUrl('https://今日头条');$r->setTimeout(10000);//10秒$r->setDelay(3);//3秒返回$r;})->find('p')->texts();print_r($data->all());开启调试模式,在本地加载cookie文件:$data=$ql->browser(function(\JonnyW\PhantomJs\Http\RequestInterface$r){$r->setMethod('GET');$r->setUrl('https://m.toutiao.com');$r->setTimeout(10000);//10秒$r->setDelay(3);//3秒返回$r;},true,['--cookies-file'=>'/path/to/cookies.txt'])->rules(['title'=>['p','text'],'link'=>['a','href']])->query()->getData();print_r($data->all());
