- <?php
- php_sapi_name() === 'cli' or exit('Please use the cli environemnt!' . PHP_EOL);
- $page = 1;
- $max_page = 5;
- $base_url = 'http://sexy.faceks.com/?page=';
- function getLinks($html){
- preg_match_all('/<a class="img" href="(.*)">[\\s]*<img src=".*" \\/>[\\s]*<\\/a>[\\s]*<\\/div>[\\s]*<div class="text"><p>(.*)<br \\/><\\/p>/', $html, $matchs);
- $links = array();
- if(count($matchs) > 2){
- foreach ($matchs[2] as $k => $v) {
- $links[$v] = $matchs[1][$k];
- }
- }
- return $links;
- }
- function getImgUrls($url){
- $html = file_get_contents($url);
- preg_match_all('<img src="(.*)">', $html, $matchs);
- return count($matchs) > 1 ? $matchs[1] : null;
- }
- function getImage($path, $url){
- preg_match('/==\\/(\\w+)\\.jpg/', $url, $match);
- $filepath = $path . DIRECTORY_SEPARATOR . $match[1] . '.jpg';
- ob_start();
- readfile($url);
- $img = ob_get_contents();
- ob_end_clean();
- $fp=@fopen($filepath, 'a') ;
- fwrite($fp, $img);
- fclose($fp) ;
- }
- while($page <= $max_page){
- echo '>>> Start download page ' . $page . PHP_EOL;
- $url = $base_url . $page;
- $html = file_get_contents($url);
- $links = getLinks($html);
- echo '>>> Find ' . count($links) . ' atlas' . PHP_EOL;
- foreach ($links as $k => $v) {
- $k = str_replace(' ', ' ', $k);
- if(!file_exists($k)){
- echo '>>> Make directory ' . $k . PHP_EOL;
- mkdir($k, 0755, true);
- }
- $img_urls = getImgUrls($v);
- foreach ($img_urls as $key => $value) {
- echo '>>> Downloading ' . $value . PHP_EOL;
- getImage($k, $value);
- }
- }
- $page++;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2702201511803.html
来源: http://www.codesnippet.cn/detail/2702201511803.html