Home > Backend Development > PHP Tutorial > PHP抓取并保存网页所有图片

PHP抓取并保存网页所有图片

WBOY
Release: 2016-06-20 12:59:42
Original
1002 people have browsed it

废话不说,直接上代码

<?phpinclude_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;}?>
Copy after login

simple_html_dom下载地址

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template