php上手超快的框架querylist4
文档地址:安装 - QueryList文档
安装tp5.1,composer命令安装
composer create-project topthink/think tp5
安装querylist4(环境php>7.0 )
composer require jaeger/querylist
直接在控制器开撸
搞几个例子吧:
/*** 采集某页面所有的图片*/public function test(){//采集某页面所有的图片$data = QueryList::get('/bizhi/453.html')->find('img')->attrs('src');//打印结果print_r($data->all());}/*** 多线程*/public function getList(){$urls = ['/trending/go?since=daily','/trending/html?since=daily','/trending/java?since=daily'];QueryList::multiGet($urls)->success(function (QueryList $ql, Response $response, $index) use ($urls) {echo 'Current url: ' . $urls[$index] . "\r\n";$data = $ql->find('h3>a')->texts();print_r($data->all());})->send();}/*** 获取html*/public function getHtml(){$ql = QueryList::get('/');echo $ql->getHtml();}/*** 获取表格元素*/public function table(){//获取html$ql = QueryList::get('/css/#tables');$html = $ql->getHtml();$table = QueryList::html($html)->find('table');// 采集表头$tableHeader = $table->find('tr:eq(0)')->find('td')->texts();// 采集表的每行内容$tableRows = $table->find('tr:gt(0)')->map(function ($row) {return $row->find('td')->texts()->all();});print_r($tableHeader->all());print_r($tableRows->all());}/*** 获取部分元素*/public function part(){//获取html$ql = QueryList::get('/');$html = $ql->getHtml();$ql = QueryList::html($html);$rt = [];//获取第一张图片的链接地址//下面四种写法完全等价$rt[] = $ql->find('img')->attr('src');$rt[] = $ql->find('img')->src;$rt[] = $ql->find('img:eq(0)')->src;$rt[] = $ql->find('img')->eq(0)->src;//获取第一张图片的alt属性$rt[] = $ql->find('img')->alt;//获取第一张图片的abc属性,注意这里获取定义属性的写法与普通属性的写法是一样的$rt[] = $ql->find('img')->abc;print_r($rt);}