編程是枯燥、乏味的,想要讓編程變的有意思忘分,需要做一些好玩的小實驗,才更有興趣去學(xué)習(xí)
話不過說白修,直接代碼
<?php
set_time_limit(0);
$url="http://www.enterdesk.com/special/wmyjtp/";//填寫爬取的網(wǎng)站URL地址
$str=file_get_contents($url);
$reg='/<\s*img\s+[^>]*?src="([^"]*)"/i'; //正則的匹配
preg_match_all($reg, $str, $matches);
var_dump($matches);
foreach ($matches[0] as $key => $value) {
$reg = '/http:\/\/.*.(jpg|png)/';
preg_match_all($reg, $value, $match);
if($match[1][0]=='jpg') {
$str=file_get_contents($match[0][0]);
$name = 'F:/img/'.$key.'.jpg';//選擇存放的路徑
@mkdir(dirname($name));
file_put_contents($name, $str);
}elseif($match[1][0]=='png') {
$str=file_get_contents($match[0][0]);
$name = 'F:/img/'.$key.'.png';
@mkdir(dirname($name));
file_put_contents($name, $str);
}else{
echo? "other imges types";
}
}
?>
注意:不過有個小問題妒峦,當(dāng)前的代碼只支持http協(xié)議
附加一張圖片