php 下载前10页的暴走漫画

<?php
/**
 * 正则表达式匹配需要的图片
 * @return ArrayIterator $arr
 */
function pregIMG($str){
	
	$pattern='/<img.+src=\"(.+large.+\.jpg)\"?.+>/i';
	preg_match_all($pattern,$str,$matchs);
	return $matchs;
}
/**
 * 查找某个页面的jpg
 */
function getIMG($url,$dir){

	if (empty($url)) return false;
	$contents = file_get_contents($url);
	$data = pregIMG($contents);

	$jpgs = $data[1];
	
	unset($data);
	
	$data = $jpgs;
	
	if(!file_exists($dir) || !is_dir($dir)){
		mkdir($dir,0777);
	}
	$num = 1;
	foreach ($data as $v){
		
		copy($v, $dir.'/'.time(). rand(10,100).'.jpg');
		usleep(1);
		$num++;
	}
	return $num;
}
/**
 *	获取给定页数页数的暴漫
 */
function getBaoZou($page=1){
	$url = "http://baozoumanhua.com/tucao/fresh/page/".$page."?sv=1395827058";
	$dir = "public";
	return getIMG($url, $dir);
}
function getMoreBaoZou($page){
	//下载前10页
	$num = 0;
	for ($i = 1;$i < $page; $i++){
		$num += getBaoZou($i);
	}
	echo '一共下载了'.$num.'张暴走漫画';//未设置页编码,可能会乱码,请自行设定
}
getMoreBaoZou(10);//获取前10页的暴走漫画
?>


你可能感兴趣的:(PHP,正则,暴走漫画)