<?php namespace app\api\controller; use app\api\model\User; use GuzzleHttp\Client; use QL\QueryList; class Index { public function index() { return '您好!这是一个[api]示例应用'; } public function hello() { return User::value('id'); } public function pc() { $data = QueryList::get('https://www.baidu.com/s?wd=php') // 设置采集规则 ->rules([ 'title'=>array('h3','text'), 'link'=>array('h3>a','href'), 'content'=>array('.c-abstract','text'), 'img'=>array('.c-gap-top-small img','src'), ]) ->queryData(); print_r($data); //爬取表格 $client = new Client(); $res = $client->request('GET', 'http://juhe.xin88.top/admin.php/orders/index.html', [ 'wd' => 'QueryList', 'headers' => ['Cookie' => 'thinkphp_show_page_trace=0|0; UM_distinctid=16bc66fd81ffb-01cdf64795fa2b-5f1d3a17-1fa400-16bc66fd82093e; PHPSESSID=r5qdgj4h914msjfea4c4km640v; Hm_lvt_afc2b8bbe5afca7b1c77c05445eadd98=1561977384,1562211145,1562555307,1562722658; thinkphp_show_page_trace=0|0; CNZZDATA1277781990=1926222929-1562738816-http%253A%252F%252Flocalhost%252F%7C1562910416; Hm_lpvt_afc2b8bbe5afca7b1c77c05445eadd98=1562912617' ] ]); $html = (string)$res->getBody(); // print_r($html);die; $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()); } }
QueryList爬取数据
最新推荐文章于 2024-04-18 16:07:31 发布