下面來演示QueryList使用PhantomJS插件抓取JS動(dòng)態(tài)創(chuàng)建的頁(yè)面內(nèi)容。
推薦:《PHP教程》
安裝
使用Composer安裝:
安裝QueryList
composer require jaeger/querylistGitHub: https://github.com/jae-jae/QueryList
安裝PhantomJS插件
composer require jaeger/querylist-phantomjsGitHub: https://github.com/jae-jae/QueryList-PhantomJS
下載PhantomJS二進(jìn)制文件
PhantomJS官網(wǎng):https://phantomjs.org ,下載對(duì)應(yīng)平臺(tái)的PhantomJS二進(jìn)制文件。
插件API
QueryList browser($url,$debug = false,$commandOpt = []):使用瀏覽器打開連接
使用
以采集「今日頭條」手機(jī)版為例,「今日頭條」手機(jī)版基于React框架,內(nèi)容是純動(dòng)態(tài)渲染出來的。
下面演示QueryList的PhantomJs插件用法:
安裝插件
use QL/QueryList;use QL/Ext/PhantomJs;$ql = QueryList::getInstance();// 安裝時(shí)需要設(shè)置PhantomJS二進(jìn)制文件路徑$ql->use(PhantomJs::html' target='_blank'>class,'/usr/local/bin/phantomjs');//or Custom function name$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');
Example-1
獲取動(dòng)態(tài)渲染的HTML:
$html = $ql->browser('https://m.toutiao.com')->getHtml();print_r($html);獲取所有p標(biāo)簽文本內(nèi)容:
$data = $ql->browser('https://m.toutiao.com')->find('p')->texts();print_r($data->all());輸出:
Array( [0] => 自拍模式開啟!國(guó)慶假期我和國(guó)旗合個(gè)影 [1] => 你旅途已開始 他們?nèi)栽谧约旱膷徫簧蠟槟愕募倨诒q{護(hù)航 [2] => 喜極而泣,都教授終于回到地球了! //....)使用http代理:
// 更多選項(xiàng)可以查看文檔: https://phantomjs.org/api/command-line.html$ql->browser('https://m.toutiao.com',true,[ // 使用http代理 '--proxy' => '192.168.1.42:8080', '--proxy-type' => 'http'])Example-2
自定義一個(gè)復(fù)雜的請(qǐng)求:
$data = $ql->browser(function (/JonnyW/PhantomJs/Http/RequestInterface $r){ $r->setMethod('GET'); $r->setUrl('https://m.toutiao.com'); $r->setTimeout(10000); // 10 seconds $r->setDelay(3); // 3 seconds return $r;})->find('p')->texts();print_r($data->all());開啟debug模式,并從本地加載cookie文件:
$data = $ql->browser(function (/JonnyW/PhantomJs/Http/RequestInterface $r){ $r->setMethod('GET'); $r->setUrl('https://m.toutiao.com'); $r->setTimeout(10000); // 10 seconds $r->setDelay(3); // 3 seconds return $r;},true,[ '--cookies-file' => '/path/to/cookies.txt'])->rules([ 'title' => ['p','text'], 'link' => ['a','href']])->query()->getData();print_r($data->all());以上就是php使用QueryList輕松采集JavaScript動(dòng)態(tài)渲染頁(yè)面的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選