對於更複雜的http網路操作
QueryList
本身內建的網路操作非常簡單,QueryList
專注於DOM選擇;對於更複雜的網路操作可以選擇使用Request擴充
# ,它可以簡單的實現:攜帶cookie、偽造來路、偽造瀏覽器等功能,但如果覺的它依舊不能滿足你的需求,下面有幾個可以參考的方案:
#範例:
function getHtml($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); return $result; } $rules = array( //采集规则 ); //获取页面源码 $html = getHtml('http://xxx.com'); //采集 $data = QueryList::Query($html,$rules)->data;
QueryList
可以無縫與任意第三放http套件配合使用,下面以guzzlehttp/guzzle
套件為例,Guzzle
是一個PHP的HTTP客戶端,用來輕易地發送請求,並整合到我們的WEB服務上。
Guzzle
中文手冊:http://guzzle-cn.readthedocs.io/zh_CN/latest/
安裝
//安装QueryList composer require jaeger/querylist //安装Guzzle composer require guzzlehttp/guzzle
使用
<?php require 'vendor/autoload.php'; //实例化一个Http客户端 $client = new GuzzleHttp\Client(['base_uri' => 'https://phphub.org']); $jar = new \GuzzleHttp\Cookie\CookieJar(); //发送一个Http请求 $response = $client->request('GET', '/categories/6', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ], 'form_params' => [ 'foo' => 'bar', 'baz' => ['hi', 'there!'] ], // 'cookies' => $jar, 'timeout' => 3.14, // 'proxy' => 'tcp://localhost:8125', // 'cert' => ['/path/server.pem', 'password'], ]); $body = $response->getBody(); //获取到页面源码 $html = (string)$body; //采集规则 $rules = array( //文章标题 'title' => ['.media-heading a','text'], //文章链接 'link' => ['.media-heading a','href'], //文章作者名 'author' => ['.img-thumbnail','alt'] ); //列表选择器 $rang = '.topic-list>li'; //采集 $data = \QL\QueryList::Query($html,$rules,$rang)->data; //查看采集结果 print_r($data);
結果:
Array ( [0] => Array ( [title] => 好友动态的实现原理 [link] => https://phphub.org/topics/2750 [author] => luo975974740 ) [1] => Array ( [title] => 打造完美的 Ubuntu16.04 开发环境【持续更新】 [link] => https://phphub.org/topics/2723 [author] => liuwantao ) //省略........ [19] => Array ( [title] => [Laravel 5.3 新功能] 10. 全文搜索方案 Laravel Scout 介绍 [link] => https://phphub.org/topics/2673 [author] => monkey ) )