QueryList入门
生活随笔
收集整理的這篇文章主要介紹了
QueryList入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QueryList入門
規則庫的編寫
$rules = array('規則名' => array('jQuery選擇器','要采集的屬性'),'規則名2' => array('jQuery選擇器','要采集的屬性'),.......... );利用編寫的規則庫,收集對應的元素
<?php namespace app\index\controller; //require 'vendor/autoload.php';//導入QueryList的庫 use QL\QueryList;class Index {public function index(){//把采集目標放在html變量中,包含在STR里$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'));//使用QueryList中的Query(參數1 采集內容,參數2 采集規則)方法進行采集,在方法中獲取了data變量$data = QueryList::Query($html,$rules)->data;//打印結果print_r($data);} }結果
Array ( [0] => Array ( [text] => QueryList官網 其它的一些文本 [link] => http://querylist.cc [img] => http://querylist.com/2.jpg [other] => 其它的一些文本 ) )采集文章頁
<?php namespace app\index\controller; //require 'vendor/autoload.php';use QL\QueryList;class Index {public function demo2(){// 待采集的頁面地址$url = 'https://www.cnbeta.com/articles/tech/779841.htm';// 采集規則$rules = [// 文章標題'title' => ['.title>h1', 'text'],// 發布日期'date' => ['.meta>span:eq(0)', 'text'],// 文章內容'content' => ['#artibody', 'html']];$data = QueryList::Query($url, $rules)->data;print_r($data);} }總結
以上是生活随笔為你收集整理的QueryList入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机专业教育论述,计算机专业远程开放教
- 下一篇: 崩坏3区号+86_科普:手机号码前的“+