首頁 > 後端開發 > php教程 > PHP抓取網站圖片腳本

PHP抓取網站圖片腳本

WBOY
發布: 2016-07-29 09:14:50
原創
1249 人瀏覽過

方法一:

<code><span><?php</span>
header(<span>"Content-type:image/jpeg"</span>); 
<span><span>class</span><span>download_image</span>{</span><span><span>function</span><span>read_url</span><span>(<span>$str</span>)</span>    {</span><span>$file</span>=fopen(<span>$str</span>,<span>"r"</span>);
        <span>$result</span> = <span>''</span>; 
        <span>while</span>(!feof(<span>$file</span>)) 
        { 
            <span>$result</span>.=fgets(<span>$file</span>,<span>9999</span>); 
        } 
        fclose(<span>$file</span>); 
        <span>return</span><span>$result</span>; 

    } 
    <span><span>function</span><span>save_img</span><span>(<span>$str</span>)</span>    {</span><span>$result</span>=<span>$this</span>->read_url(<span>$str</span>); 
        <span>$result</span>=str_replace(<span>"\""</span>,<span>""</span>,<span>$result</span>); 
        <span>$result</span>=str_replace(<span>"\'"</span>,<span>""</span>,<span>$result</span>); 

        preg_match_all(<span>'/<img\ssrc=(http:\/\/.*?)(\s(.*?)>|>)/i'</span>,<span>$result</span>,<span>$matches</span>); 

        <span>foreach</span>(<span>$matches</span>[<span>1</span>] <span>as</span><span>$value</span>) 
        { 
            <span>echo</span><span>$value</span>.<span>"\n"</span>; 
            <span>$this</span>->GrabImage(<span>$value</span>,<span>$filename</span>=<span>""</span>); 
        } 
    } 
    <span>// $url 是远程图片的完整URL地址,不能为空。 </span><span>// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期 </span><span>// 自动生成. </span><span><span>function</span><span>GrabImage</span><span>(<span>$url</span>,<span>$filename</span>=<span>""</span>)</span> {</span><span>if</span>(<span>$url</span>==<span>""</span>):<span>return</span><span>false</span>;<span>endif</span>; 
        <span>$path</span>=<span>"download/"</span>; <span>//指定存储文件夹 </span><span>//若文件不存在,则创建; </span><span>if</span>(!file_exists(<span>$path</span>)){ 
            mkdir(<span>$path</span>); 
        } 

        <span>if</span>(<span>$filename</span>==<span>""</span>) { 
            <span>$ext</span>=strrchr(<span>$url</span>,<span>"."</span>); 
            <span>if</span>(<span>$ext</span>!=<span>".gif"</span> && <span>$ext</span>!=<span>".jpg"</span>):<span>return</span><span>false</span>;<span>endif</span>; 
            @<span>$filename</span>=<span>$path</span>.date(<span>"YHis"</span>).<span>$ext</span>; 
        } 

        ob_start(); 
        readfile(<span>$url</span>); 
        <span>$img</span> = ob_get_contents(); 
        ob_end_clean(); 
        <span>$size</span> = strlen(<span>$img</span>); 

        <span>$fp2</span>=@fopen(<span>$filename</span>, <span>"a"</span>); 
        fwrite(<span>$fp2</span>,<span>$img</span>); 
        fclose(<span>$fp2</span>); 

        <span>return</span><span>$filename</span>; 
    }
}
<span>$download_img</span>=<span>new</span> download_image();
<span>$download_img</span>->save_img(<span>"http://www.jb51.net"</span>); 
<span>?></span></span></code>
登入後複製

方法一想法比較簡單清晰,不過有個BUG,圖片沒有完全抓取,有時間再檢查看!

方法二:

<code><span><span><?php</span><span><span>class</span><span>download_image</span>{</span><span>//抓取图片的保存地址</span><span>public</span><span>$save_path</span>;   
    <span>//抓取图片的大小限制(单位:字节) 只抓比size比这个限制大的图片</span><span>public</span><span>$img_size</span>=<span>0</span>; 
    <span>//定义一个静态数组,用于记录曾经抓取过的的超链接地址,避免重复抓取       </span><span>public</span><span>static</span><span>$a_url_arr</span>=<span>array</span>();

    <span>/**
     *<span> @param</span> String $save_path    抓取图片的保存地址
     *<span> @param</span> Int    $img_size     
     */</span><span>public</span><span><span>function</span><span>__construct</span><span>(<span>$save_path</span>,<span>$img_size</span>)</span>{</span><span>$this</span>->save_path=<span>$save_path</span>;
        <span>$this</span>->img_size=<span>$img_size</span>;
        <span>if</span>(!file_exists(<span>$save_path</span>)){
            mkdir(<span>$save_path</span>,<span>0775</span>);
        }
    }
    <span>/**
     * 递归下载抓取首页及其子页面图片的方法  ( recursive 递归)
     *<span> @param</span>   String  $capture_url  用于抓取图片的网址
     */</span><span>public</span><span><span>function</span><span>recursive_download_images</span><span>(<span>$capture_url</span>)</span>{</span><span>if</span> (!in_array(<span>$capture_url</span>,<span>self</span>::<span>$a_url_arr</span>)){   <span>//没抓取过</span><span>self</span>::<span>$a_url_arr</span>[]=<span>$capture_url</span>;   <span>//计入静态数组</span>
        } <span>else</span> {   <span>//抓取过,直接退出函数</span><span>return</span>;
        }
        <span>$this</span>->download_current_page_images(<span>$capture_url</span>);  <span>//下载当前页面的所有图片</span><span>//用@屏蔽掉因为抓取地址无法读取导致的warning错误</span><span>$content</span>=@file_get_contents(<span>$capture_url</span>); 
        <span>//匹配<strong>a标签</strong>href属性中?之前部分的正则</span><span>$a_pattern</span> = <span>"|<a[^>]+href=['\" ]?([^ '\"?]+)['\" >]|U"</span>;   
        preg_match_all(<span>$a_pattern</span>, <span>$content</span>, <span>$a_out</span>, PREG_SET_ORDER);
        <span>$tmp_arr</span>=<span>array</span>();  <span>//定义一个数组,用于存放当前<strong>循环</strong>下抓取图片的超链接地址</span><span>foreach</span> (<span>$a_out</span><span>as</span><span>$k</span> => <span>$v</span>) {
            <span>/**
             * 去除超链接中的 空'','#','/'和重复值  
             * 1: 超链接地址的值 不能等于当前抓取页面的url, 否则会陷入死<strong>循环</strong>
             * 2: 超链接为''或'#','/'也是本页面,这样也会陷入死<strong>循环</strong>,  
             * 3: 有时一个超连接地址在一个网页中会重复出现多次,如果不去除,会对一个子页面进行重复下载)
             */</span><span>if</span> ( <span>$v</span>[<span>1</span>] && !in_array(<span>$v</span>[<span>1</span>],<span>self</span>::<span>$a_url_arr</span>) &&!in_array(<span>$v</span>[<span>1</span>],<span>array</span>(<span>'#'</span>,<span>'/'</span>,<span>$capture_url</span>) ) ) { 
                <span>$tmp_arr</span>[]=<span>$v</span>[<span>1</span>];
            }
        }
        <span>foreach</span> (<span>$tmp_arr</span><span>as</span><span>$k</span> => <span>$v</span>){ 
            <span>//超链接路径地址</span><span>if</span> ( strpos(<span>$v</span>, <span>'http://'</span>)!==<span>false</span> ){ <span>//如果url包含http://,可以直接访问</span><span>$a_url</span> = <span>$v</span>;
            }<span>else</span>{   <span>//否则证明是相对地址, 需要重新拼凑超链接的访问地址</span><span>$domain_url</span> = substr(<span>$capture_url</span>, <span>0</span>,strpos(<span>$capture_url</span>, <span>'/'</span>,<span>8</span>)+<span>1</span>);
                <span>$a_url</span>=<span>$domain_url</span>.<span>$v</span>;
            }
            <span>$this</span>->recursive_download_images(<span>$a_url</span>);
        }
    }
    <span>/**
     * 下载当前网页下的所有图片 
     *<span> @param</span>   String  $capture_url  用于抓取图片的网页地址
     *<span> @return</span>  Array   当前网页上所有图片img标签url地址的一个数组
     */</span><span>public</span><span><span>function</span><span>download_current_page_images</span><span>(<span>$capture_url</span>)</span>{</span><span>$content</span>=@file_get_contents(<span>$capture_url</span>);   <span>//屏蔽warning错误</span><span>//匹配img标签src属性中?之前部分的正则</span><span>$img_pattern</span> = <span>"|<img[^>]+src=['\" ]?([^ '\"?]+)['\" >]|U"</span>;   
        preg_match_all(<span>$img_pattern</span>, <span>$content</span>, <span>$img_out</span>, PREG_SET_ORDER);
        <span>$photo_num</span> = count(<span>$img_out</span>);
        <span>//匹配到的图片数量</span><span>echo</span><span>$capture_url</span> . <span>"共找到 "</span> . <span>$photo_num</span> . <span>" 张图片\n"</span>;
        <span>foreach</span> (<span>$img_out</span><span>as</span><span>$k</span> => <span>$v</span>){
            <span>$this</span>->save_one_img(<span>$capture_url</span>,<span>$v</span>[<span>1</span>]);
        }
    }

    <span>/**
     * 保存单个图片的方法 
     *<span> @param</span> String $capture_url   用于抓取图片的网页地址
     *<span> @param</span> String $img_url       需要保存的图片的url
     */</span><span>public</span><span><span>function</span><span>save_one_img</span><span>(<span>$capture_url</span>,<span>$img_url</span>)</span>{</span><span>//图片路径地址</span><span>if</span> ( strpos(<span>$img_url</span>, <span>'http://'</span>)!==<span>false</span> ){ 
            <span>// $img_url = $img_url;</span>
        }<span>else</span>{   
            <span>$domain_url</span> = substr(<span>$capture_url</span>, <span>0</span>,strpos(<span>$capture_url</span>, <span>'/'</span>,<span>8</span>)+<span>1</span>);
            <span>$img_url</span>=<span>$domain_url</span>.<span>$img_url</span>;
        }           
        <span>$pathinfo</span> = pathinfo(<span>$img_url</span>);    <span>//获取图片路径信息        </span><span>$pic_name</span>=<span>$pathinfo</span>[<span>'basename'</span>];   <span>//获取图片的名字</span><span>if</span> (file_exists(<span>$this</span>->save_path.<span>$pic_name</span>)){  <span>//如果图片存在,证明已经被抓取过,退出函数</span><span>echo</span><span>$img_url</span>.<span>'该图片已经抓取过!'</span>.<span>"\n"</span>; 
            <span>return</span>;
        }                
        <span>//将图片内容读入一个字符串</span><span>$img_data</span> = @file_get_contents(<span>$img_url</span>);   <span>//屏蔽掉因为图片地址无法读取导致的warning错误</span><span>if</span> ( strlen(<span>$img_data</span>) > <span>$this</span>->img_size ){   <span>//下载size比限制大的图片</span><span>$img_size</span> = file_put_contents(<span>$this</span>->save_path . <span>$pic_name</span>, <span>$img_data</span>);
            <span>if</span> (<span>$img_size</span>){
                <span>echo</span><span>$img_url</span>.<span>'图片保存成功!'</span>.<span>"\n"</span>;
            } <span>else</span> {
                <span>echo</span><span>$img_url</span>.<span>'图片保存失败!'</span>.<span>"\n"</span>;
            }
        } <span>else</span> {
            <span>echo</span><span>$img_url</span>.<span>'图片读取失败!'</span>.<span>"\n"</span>;
        } 
    } 
}
set_time_limit(<span>120</span>);     <span>//设置脚本的最大执行时间  根据情况设置 </span><span>$download_img</span>=<span>new</span> download_image(<span>'imgages/'</span>,<span>0</span>);   <span>//实例化下载图片<strong>对象</strong></span><span>//$download_img->recursive_download_images('http://www.oschina.net/');      //递归抓取图片方法</span><span>//$download_img->download_current_page_images($_POST['capture_url']);     //只抓取当前页面图片方法</span><span>$download_img</span>->download_current_page_images(<span>'http://www.jb51.net'</span>);     <span>//只抓取当前页面图片方法</span><span>?></span></span></span></code>
登入後複製

http://blog.csdn.net/china_skag/article/details/18452883
http://www.jb51.net/article/21738.htm

版權聲明:轉載請保留文章署名和連結

以上就介紹了PHP抓取網站圖片腳本,包括了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板