QueryList使用jQuery的方式來做采集,擁有豐富的插件,
下面來演示QueryList使用PhantomJS插件抓取JS動態創建的頁面內容,
安裝
使用Composer安裝:
安裝QueryList
|
1
2
|
composer require jaeger/querylist
GitHub: https://github.com/jae-jae/QueryList
|
安裝PhantomJS插件
|
1
2
|
composer require jaeger/querylist-phantomjs
GitHub: https://github.com/jae-jae/QueryList-PhantomJS
|
下載PhantomJS二進制檔案
PhantomJS官網:http://phantomjs.org ,下載對應平臺的PhantomJS二進制檔案,
插件API
QueryList browser($url,$debug = false,$commandOpt = []):使用瀏覽器打開連接
使用
以采集「今日頭條」手機版為例,「今日頭條」手機版基于React框架,內容是純動態渲染出來的,
下面演示QueryList的PhantomJs插件用法:
安裝插件
|
1
2
3
4
5
6
7
|
use QL\QueryList;
use QL\Ext\PhantomJs;
$ql = QueryList::getInstance();
// 安裝時需要設定PhantomJS二進制檔案路徑
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs');
//or Custom function name
$ql->use(PhantomJs::class,'/usr/local/bin/phantomjs','browser');
|
Example-1
獲取動態渲染的HTML:
|
1
2
|
$html = $ql->browser('https://m.toutiao.com')->getHtml();
print_r($html);
|
獲取所有p標簽文本內容:
|
1
2
|
$data = $ql->browser('https://m.toutiao.com')->find('p')->texts();
print_r($data->all());
|
輸出:
|
1
2
3
4
5
6
7
|
Array
(
[0] => 自拍模式開啟!國慶假期我和國旗合個影
[1] => 你旅途已開始 他們仍在自己的崗位上為你的假期保駕護航
[2] => 喜極而泣,都教授終于回到地球了!
//....
)
|
使用http代理:
|
1
2
3
4
5
6
|
// 更多選項可以查看檔案: http://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
自定義一個復雜的請求:
|
1
2
3
4
5
6
7
8
|
$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檔案:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$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());
|

明確的學習思路能更高效的學習

點此加入該群學習
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/119134.html
標籤:PHP
上一篇:Swoole RPC 的實作
下一篇:qt代碼除錯