日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php爬虫QueryList使用

發布時間:2023/12/29 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php爬虫QueryList使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、QueryList手冊:https://www.php.cn/course/371.html

? ? ? QueryList V4版本文檔:?https://querylist.cc/docs/guide/v4/

2、下載QueryList:https://www.php.cn/xiazai/leiku/308,解壓得到phpQuery.php和QueryList.php這兩個文件。

3、手動下載phpQuery.php和QueryList.php這兩個文件,然后手動引入這兩個文件就可以了。
?

<?php require?'phpQuery.php'; require?'QueryList.php'; use?QL\QueryList; $hj?=?QueryList::Query('http://mobile.csdn.net/',array("url"=>array('.unit?h1?a','href'))); $data?=?$hj->getData(function($x){return?$x['url']; }); print_r($data);

4、使用QueyList只需要編寫規則庫,然后把規則庫傳給QueryList的靜態方法Query,QueryList就會自動按照規則庫把內容全部采集回來了,而規則庫是用jQuery選擇器來編寫的,所以使用QueryList的整個過程非常簡單!

規則庫的編寫規則如下(簡單模式):

$rules?=?array('規則名'?=>?array('jQuery選擇器','要采集的屬性'),'規則名2'?=>?array('jQuery選擇器','要采集的屬性'),.......... );

下面我們來動手試試吧:

(1)采集目標,下面的代碼片段

//引入文件 require 'phpQuery.php'; require 'QueryList.php'; //使用QL命名空間下的QueryList類 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;

(2)編寫采集規則

$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)開始采集

$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]?=>?其它的<b>一些</b>文本) )

5、API

(1)Query()方法

Query方法為QueryList唯一的主方法,用靜態的方式調用。

原型:

QueryList::Query($page,array $rules, $range = ‘’, $outputEncoding = null, $inputEncoding = null,$removeHead = false)

參數解釋:

  • $page 采集的目標頁面

類型:string
要抓取的網頁URL地址(支持https);或者是html代碼片段

  • $rules 采集規則

類型:array

  • $range 區域選擇器 (可選)

類型:string
默認值:''

區域選擇器或者說范圍選擇器,指 先按照規則 選出 幾個大塊 ,然后再分別再在塊里面 進行相關的選擇。當采集列表的時候,建議設置這個參數。

查看區域選擇器例子:http://doc.querylist.cc/site/index/doc/29

  • $outputEncoding 輸出編碼(可選)

類型:string
默認值:null

指要以什么編碼輸出(UTF-8,GB2312,…..),防止出現亂碼,如果設置null則不改變原字符串編碼

  • $inputEncoding 輸入編碼(可選)

類型:string
默認值:null

明確指定輸入的頁面編碼格式(UTF-8,GB2312,…..),防止出現亂碼,如果設置null則自動識別

  • $removeHead ?是否移除頭部(可選)

類型:bool
默認值:false

是否移除頁面頭部區域,亂碼終極解決方案。
注意:當這個參數設置為true的時候,無法選擇頁面中head區域里面的內容。

(2)getData( ) 方法

獲取采集結果數據的結果數據,并可以進一步處理結果。

補全圖片鏈接,改造采集代碼:

$baseUrl = 'http://xxxx.com'; $data = QueryList::Query($html,array('image' => array('.two>img','src')))->getData(function($item) use($baseUrl){return $baseUrl.$item['image'];}); print_r($data);

(3)getHtml( ) 方法

返回值:string

獲取目標頁面源碼,主要用于調試。

(4)setQuery( ) 方法

返回值:QueryList對象

重新設置選擇器,不會再次重復的取抓取一遍目標頁面源碼,用于重復采集同一個頁面多處的內容。

原型:

setQuery(array $rules, $range = ‘’,$outputEncoding = null, $inputEncoding = null,$removeHead = false)

參數解釋同Query

//采集文本 $ql = QueryList::Query($html,array('txt' => array('span:eq(0)','text'))); print_r($ql->data); //采集圖片 $ql->setQuery(array('image' => array('.xx img','src') )); print_r($ql->data); 采集結果: Array ([0] => Array([txt] => xxxxxxxx) ) Array ([0] => Array([image] => /path/to/1.jpg) ) **/

(5)run( ) 方法

返回值:需查看對應的插件文檔

運行QueryList擴展

原型:

run($class,$args = array())

參數: $class

類型:string

插件名稱

參數: $args

類型:array

傳遞給插件的參數

$login?=?QueryList::run('Login',['target'?=>?'http://xxx.com/login.php','method'?=>?'post','params'?=>?['username'=>'admin','password'=>'admin'],'cookiePath'?=>?'./cookie123.txt']); //........

(6)getInstance( ) 方法

返回值:實例對象

獲取任意類的單例,QueryList內部方法,開放出來供大家使用。

原型:

getInstance($class,$arg1,$arg2,……)

參數: $class

類型:string

包含命名空間的類名

參數:$arg1,$arg2,……

可傳遞任意多個參數


用法

運行下面例子需要先安裝Http庫:

composer?require?jaeger/http ???????<?php require?'vendor/autoload.php'; use?QL\QueryList; $http?=?QueryList::getInstance(QL\Ext\Lib\Http::class); $html?=?$http->get('http://www.baidu.com'); print_r($html);

?

總結

以上是生活随笔為你收集整理的php爬虫QueryList使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。