欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php使用QueryList輕松采集js動態(tài)渲染頁面方法

 更新時間:2018年09月11日 08:22:19   投稿:laozhang  
這篇文章給大家分享了php使用QueryList輕松采集js動態(tài)渲染頁面的相關(guān)知識點內(nèi)容,有興趣的朋友學(xué)習(xí)下。

QueryList使用jQuery的方式來做采集,擁有豐富的插件。下面來演示QueryList使用PhantomJS插件抓取JS動態(tài)創(chuàng)建的頁面內(nèi)容。

一、安裝

使用Composer安裝:

1.安裝QueryList

composer require jaeger/querylist

GitHub: https://github.com/jae-jae/QueryList

2.安裝PhantomJS插件

composer require jaeger/querylist-phantomjs

GitHub: https://github.com/jae-jae/QueryList-PhantomJS

二、下載PhantomJS二進(jìn)制文件

PhantomJS官網(wǎng):http://phantomjs.org ,下載對應(yīng)平臺的PhantomJS二進(jìn)制文件。

三、插件API

QueryList browser($url,$debug = false,$commandOpt = []):使用瀏覽器打開連接

四、使用

以采集「今日頭條」手機版為例,「今日頭條」手機版基于React框架,內(nèi)容是純動態(tài)渲染出來的。

下面演示QueryList的PhantomJs插件用法:

1.安裝插件

use QL\QueryList;
use QL\Ext\PhantomJs;
 
$ql = QueryList::getInstance();
// 安裝時需要設(shè)置PhantomJS二進(jìn)制文件路徑
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');
//or Custom function name
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');

2.Example-1

獲取動態(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] => 自拍模式開啟!國慶假期我和國旗合個影
  [1] => 你旅途已開始 他們?nèi)栽谧约旱膷徫簧蠟槟愕募倨诒q{護航
  [2] => 喜極而泣,都教授終于回到地球了!  //....)

使用http代理:

// 更多選項可以查看文檔: 
http://phantomjs.org/api/command-line.html
$ql->browser('https://m.toutiao.com',true,[  
// 使用http代理 
'--proxy' => '192.168.1.42:8080',  '--proxy-type' => 'http'
])

3.Example-2

自定義一個復(fù)雜的請求:

$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());

相關(guān)文章

最新評論