- <?php
- /**
- * 正则表达式匹配需要的图片
- * @return ArrayIterator $arr
- */
- function pregIMG($str){
- //$str ='<a href="/articles/7442969" target="_blank"><img alt="没标题" src="http://ww3.sinaimg.cn/large/ddc635dfjw1eet68xwz1yj20dw0h475n.jpg" style="width:460px" /></a>';
- $pattern='/<img.+src=\\"(.+\\.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 = array();
- foreach ($jpgs as $k => $v){
- $t = explode('/', $v);
- if ($t[3] == 'large'){
- $data[$k] = $v;
- }
- }
- 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页的暴走漫画
- ?>
- //该片段来自于http://www.codesnippet.cn/detail/080420149260.html
来源: http://www.codesnippet.cn/detail/080420149260.html