getData() Dapatkan hasil pengumpulan
getData() kaedah
nilai pulangan: array
获取采集结果数据的结果数据,并可以进一步处理结果。
原型:
getData($callback = null)
参数: $callback
类型:callback
默认值:null
Prototaip:getData($callback = null)
Parameter: $callback
Jenis: .
PenggunaanKumpul semua pautan gambar, sasaran koleksi:
$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 ) ) **/
Tambah keperluan keluaran tatasusunan
:
$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 ) **/
$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);