本文執行個體講述了CodeIgniter實現從網站抓取圖片並自動下載到檔案夾裡的方法。分享給大家供大家參考。具體如下:
因為某網站看圖比較坑爹,要一頁一頁的翻頁。。。。所以。。。。就寫了這麼個東西
(我是產品不是程式員)運行速度簡直無法忍受,而且經常會有錯誤發生,所以希望大家幫忙改進(PHP)。
當然也歡迎看到PYTHON,GOLANG的版本~~^_^
1. controllers:
$this->load->helper('date');$this->load->helper('phpQuery');//我是把phpQuery單檔案放到helper裡了
2. view:
這裡只是為了快速出產品,所以直接在VIEW裡寫的代碼,請無視變數名。。。
<?php$imageslist = phpQuery::newDocumentFile('http://g.e-hentai.org/g/xxxxx/xxxxxxx/');//首頁$pn = 1;//頁數$ps = $imageslist->find('.ptt td a');//從首頁抓頁面導航//從頁面導航開始擷取頁面內容foreach($ps as $p){ echo '第'.$pn++.'頁:attr("href").'"/>
'; $imagesnow = phpQuery::newDocumentFile(pq($p)->attr("href")); //單頁內容 $images = $imagesnow->find('#gdt a');//抓取圖片頁列表 foreach($images as $image) { echo 'attr("href").'"/>
'; $imagebigs = phpQuery::newDocumentFile(pq($image,$imagesnow)->attr("href")); //擷取單圖片頁地址 echo 'find('#i3 img')->attr('src').'">
';//輸出圖片 ob_start(); readfile($imagebigs->find('#i3 img')->attr('src')); $img = ob_get_contents(); ob_end_clean(); $filename='img/'.now().'.jpg'; $f=fopen($filename,'a'); fwrite($f,$img); fclose($f); }}?>
希望本文所述對大家基於CodeIgniter的php程式設計有所協助。