start using
To use QueyList, you only need to write a rule base, and then pass the rule base to the static method Query of QueryList. QueryList will automatically collect all the content back according to the rule base, and the rule base is written with jQuery selector. , so the whole process of using QueryList is very simple!
The rules for writing the rule base are as follows (simple mode):
$rules = array( '规则名' => array('jQuery选择器','要采集的属性'), '规则名2' => array('jQuery选择器','要采集的属性'), .......... );
Let’s try it out:
- Collection target, the following code fragment
$html = <<<STR <div id="one"> <div class="two"> <a href="http://querylist.cc">QueryList官网</a> <img src="http://querylist.com/1.jpg" alt="这是图片"> <img src="http://querylist.com/2.jpg" alt="这是图片2"> </div> <span>其它的<b>一些</b>文本</span> </div> STR;
2. Write collection rules
$rules = array( //采集id为one这个元素里面的纯文本内容 'text' => array('#one','text'), //采集class为two下面的超链接的链接 'link' => array('.two>a','href'), //采集class为two下面的第二张图片的链接 'img' => array('.two>img:eq(1)','src'), //采集span标签中的HTML内容 'other' => array('span','html') );
3. Start collection
$data = QueryList::Query($html,$rules)->data; //打印结果 print_r($data);
The results are as follows:
Array ( [0] => Array ( [text] => QueryList官网 其它的一些文本 [link] => http://querylist.cc [img] => http://querylist.com/2.jpg [other] => 其它的<b>一些</b>文本 ) )
If you understand the above code, congratulations, you have successfully mastered QueryList!
The following is the complete code:
<?php require 'QueryList/vendor/autoload.php'; use QL\QueryList; $html = <<<STR <div id="one"> <div class="two"> <a href="http://querylist.cc">QueryList官网</a> <img src="http://querylist.com/1.jpg" alt="这是图片"> <img src="http://querylist.com/2.jpg" alt="这是图片2"> </div> <span>其它的<b>一些</b>文本</span> </div> STR; $rules = array( //采集id为one这个元素里面的纯文本内容 'text' => array('#one','text'), //采集class为two下面的超链接的链接 'link' => array('.two>a','href'), //采集class为two下面的第二张图片的链接 'img' => array('.two>img:eq(1)','src'), //采集span标签中的HTML内容 'other' => array('span','html') ); $data = QueryList::Query($html,$rules)->data; print_r($data);