thinkphp5 querylist 爬虫
生活随笔
收集整理的這篇文章主要介紹了
thinkphp5 querylist 爬虫
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
php上手超快的框架querylist4
文檔地址:安裝 - QueryList文檔
安裝tp5.1,composer命令安裝?
composer create-project topthink/think tp5安裝querylist4(環境php>7.0 )
composer require jaeger/querylist直接在控制器開擼
搞幾個例子吧:
/*** 采集某頁面所有的圖片*/public function test(){//采集某頁面所有的圖片$data = QueryList::get('http://cms.querylist.cc/bizhi/453.html')->find('img')->attrs('src');//打印結果print_r($data->all());}/*** 多線程*/public function getList(){$urls = ['https://github.com/trending/go?since=daily','https://github.com/trending/html?since=daily','https://github.com/trending/java?since=daily'];QueryList::multiGet($urls)->success(function (QueryList $ql, Response $response, $index) use ($urls) {echo 'Current url: ' . $urls[$index] . "\r\n";$data = $ql->find('h3>a')->texts();print_r($data->all());})->send();}/*** 獲取html*/public function getHtml(){$ql = QueryList::get('http://www.ypyunedu.com/');echo $ql->getHtml();}/*** 獲取表格元素*/public function table(){//獲取html$ql = QueryList::get('https://v3.bootcss.com/css/#tables');$html = $ql->getHtml();$table = QueryList::html($html)->find('table');// 采集表頭$tableHeader = $table->find('tr:eq(0)')->find('td')->texts();// 采集表的每行內容$tableRows = $table->find('tr:gt(0)')->map(function ($row) {return $row->find('td')->texts()->all();});print_r($tableHeader->all());print_r($tableRows->all());}/*** 獲取部分元素*/public function part(){//獲取html$ql = QueryList::get('http://www.ypyunedu.com/');$html = $ql->getHtml();$ql = QueryList::html($html);$rt = [];//獲取第一張圖片的鏈接地址//下面四種寫法完全等價$rt[] = $ql->find('img')->attr('src');$rt[] = $ql->find('img')->src;$rt[] = $ql->find('img:eq(0)')->src;$rt[] = $ql->find('img')->eq(0)->src;//獲取第一張圖片的alt屬性$rt[] = $ql->find('img')->alt;//獲取第一張圖片的abc屬性,注意這里獲取定義屬性的寫法與普通屬性的寫法是一樣的$rt[] = $ql->find('img')->abc;print_r($rt);}總結
以上是生活随笔為你收集整理的thinkphp5 querylist 爬虫的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA 基于SSM的健身俱乐部管理系统
- 下一篇: 项目管理计划的制定过程