PHP抓取并保存網頁所有圖片

wdfd 9年前發布 | 2K 次閱讀 PHP

使用simple_html_dom.php一鍵保存url下所有圖片資源

<?php
include_once('simple_html_dom.php');
$url = $argv[1];

echo "start fetching images from $url".PHP_EOL.PHP_EOL;

$data = loadData($url); $html = str_get_html($data); $images = $html->find('img'); $srcs = array(); foreach ($images as $image) { $src = $image->attr['src']; saveImg($src); $srcs[] = $src; }

echo PHP_EOL.'finish';

function loadData($url) { //useragent是為了防止淘寶等公司對腳本訪問的限制

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'MMozilla/5.0 (Windows NT 6.1; rv:36.0) Gecko/20100101 Firefox/36.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
return curl_exec($ch);

}

function saveImg($src) { $path = dirname(FILE).'/images/'.date('YmdHis').rand(100, 999); $suffix = getSuffix($src); $fileName = $path.'.'.$suffix; if (file_put_contents($fileName, file_get_contents($src))) { echo "save $src success".PHP_EOL; } }

function getSuffix($src) { $pattern = '/.+.(.+)/'; preg_match($pattern, $src, $matches); $suffix = $matches[1]; if (!in_array(strtolower($suffix), array('jpg','jpeg','png','bmp','gif'))) { $suffix = 'png'; }

return $suffix;

}

?></pre>

 本文由用戶 wdfd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!