参考一个网上的简单爬虫图片代码
不废话,直接贴代码,然后自己运行去吧,
<?php
function save_img($url,$path,$img_name){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_content = curl_exec($ch);
curl_close($ch);
if(!is_dir('./'.$path)){
mkdir('./'.$path);
}
$downloaded_file = fopen('./'.$path.'/'.$img_name.'.jpg', 'w');
fwrite($downloaded_file, $file_content);
fclose($downloaded_file);
}
$img_name = 1;
for ($pageno = 1 ; $pageno < 999; $pageno ++) {
$content = file_get_contents('http://www.haha.mx/topic/1/new/'.$pageno);
preg_match_all('/class=\"joke-main-img\" src=\"(.*?)\"/',$content,$matches);
foreach ($matches[1] as $url) {
$url = ltrim($url,"//");
save_img($url,'save_one',$img_name);
$img_name ++;
}
}
怕被封,就不放爬虫下来的图片了,你们自己慢慢欣赏吧