getData() Get the collection results
getData( ) method
Return value: array
Get the result of collecting the result data data and can further process the results.
Prototype:
getData($callback = null)
Parameters: $callback
Type:callback
Default value:null
You can use this callback function to further process the results, replace content, complete links, and download Pictures, etc.;
And you can also use QueyList in this callback function to perform nested infinite collection.
Usage
Collect all picture links, collection target:
$html =<<<STR <div class="xx"> <img data-src="/path/to/1.jpg" alt=""> </div> <div class="xx"> <img data-src="/path/to/2.jpg" alt=""> </div> <div class="xx"> <img data-src="/path/to/3.jpg" alt=""> </div> STR;
$data = QueryList::Query($html,array( 'image' => array('.xx>img','data-src') ))->getData(function($item){ return $item; }); print_r($data); /** 采集结果: Array ( [0] => Array ( [image] => /path/to/1.jpg ) [1] => Array ( [image] => /path/to/2.jpg ) [2] => Array ( [image] => /path/to/3.jpg ) ) **/
Add Requirements
The output array becomes a one-dimensional array, and the acquisition code is modified:
$data = QueryList::Query($html,array( 'image' => array('.xx>img','data-src') ))->getData(function($item){ return $item['image']; }); print_r($data); /** 采集结果: Array ( [0] => /path/to/1.jpg [1] => /path/to/2.jpg [2] => /path/to/3.jpg ) **/
Continue to add Requirements
Complete the picture link and modify the collection code:
$baseUrl = 'http://xxxx.com'; $data = QueryList::Query($html,array( 'image' => array('.xx>img','data-src') ))->getData(function($item) use($baseUrl){ return $baseUrl.$item['image']; }); print_r($data); /** 采集结果: Array ( [0] => http://xxxx.com/path/to/1.jpg [1] => http://xxxx.com/path/to/2.jpg [2] => http://xxxx.com/path/to/3.jpg ) **/
完整代码
<?php
require 'vendor/autoload.php';
use QL\QueryList;
$html =<<<STR
<div class="xx">
<img data-src="/path/to/1.jpg" alt="">
</div>
<div class="xx">
<img data-src="/path/to/2.jpg" alt="">
</div>
<div class="xx">
<img data-src="/path/to/3.jpg" alt="">
</div>
STR;
$baseUrl = 'http://xxxx.com';
$data = QueryList::Query($html,array(
'image' => array('.xx>img','data-src')
))->getData(function($item) use($baseUrl){
return $baseUrl.$item['image'];
});
print_r($data);