PHP实现远程抓取网站图片并保存在文件中
优采云 发布时间: 2022-05-04 18:00PHP实现远程抓取网站图片并保存在文件中
<p style="padding: 0.5em;max-width: 100%;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);margin-left: 8px;margin-right: 8px;box-sizing: border-box !important;word-wrap: normal !important;display: block !important;word-break: normal !important;overflow: auto !important;">/**<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 递归下载抓取首页及其子页面图片的方法 ( recursive 递归)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> *<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * @param String $capture_url 用于抓取图片的网址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> *<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> */<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> public function recursive_download_images($capture_url)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if (!in_array($capture_url,self::$a_url_arr)) //没抓取过<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> self::$a_url_arr[]=$capture_url; //计入静态数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> } else //抓取过,直接退出函数<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $this->download_current_page_images($capture_url); //下载当前页面的所有图片<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //用@屏蔽掉因为抓取地址无法读取导致的warning错误<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $content=@file_get_contents($capture_url);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //匹配a标签href属性中?之前部分的正则<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $a_pattern = "|]+href=['\" ]?([^ '\"?]+)['\" >]|U";<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> preg_match_all($a_pattern, $content, $a_out, PREG_SET_ORDER);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $tmp_arr=array(); //定义一个数组,用于存放当前循环下抓取图片的超链接地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> foreach ($a_out as $k => $v)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> /**<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 去除超链接中的 空'','#','/'和重复值<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 1: 超链接地址的值 不能等于当前抓取页面的url, 否则会陷入死循环<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 2: 超链接为''或'#','/'也是本页面,这样也会陷入死循环,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 3: 有时一个超连接地址在一个网页中会重复出现多次,如果不去除,会对一个子页面进行重复下载)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> */<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if ( $v[1] && !in_array($v[1],self::$a_url_arr) &&!in_array($v[1],array('#','/',$capture_url) ) )<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $tmp_arr[]=$v[1];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> foreach ($tmp_arr as $k => $v)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //超链接路径地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if ( strpos($v, 'http://')!==false ) //如果url包含http://,可以直接访问<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $a_url = $v;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }else //否则证明是相对地址, 需要重新拼凑超链接的访问地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $domain_url = substr($capture_url, 0,strpos($capture_url, '/',8)+1);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $a_url=$domain_url.$v;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $this->recursive_download_images($a_url);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }</p>
下载当前的所有页面
<p style="padding: 0.5em;max-width: 100%;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);margin-left: 8px;margin-right: 8px;box-sizing: border-box !important;word-wrap: normal !important;display: block !important;word-break: normal !important;overflow: auto !important;">/**<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 下载当前网页下的所有图片<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> *<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * @param String $capture_url 用于抓取图片的网页地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * @return Array 当前网页上所有图片img标签url地址的一个数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> */<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> public function download_current_page_images($capture_url)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $content=@file_get_contents($capture_url); //屏蔽warning错误<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //匹配img标签src属性中?之前部分的正则<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $img_pattern = "|]+src=['\" ]?([^ '\"?]+)['\" >]|U";<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> preg_match_all($img_pattern, $content, $img_out, PREG_SET_ORDER);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $photo_num = count($img_out);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //匹配到的图片数量<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> echo ''.$capture_url . "共找到 " . $photo_num . " 张图片";<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> foreach ($img_out as $k => $v)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $this->save_one_img($capture_url,$v[1]);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></p>
保存图片
<p style="padding: 0.5em;max-width: 100%;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);margin-left: 8px;margin-right: 8px;box-sizing: border-box !important;word-wrap: normal !important;display: block !important;word-break: normal !important;overflow: auto !important;">/**<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * 保存单个图片的方法<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> *<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * @param String $capture_url 用于抓取图片的网页地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> * @param String $img_url 需要保存的图片的url<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> *<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> */<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> public function save_one_img($capture_url,$img_url)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //图片路径地址<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if ( strpos($img_url, 'http://')!==false )<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> // $img_url = $img_url;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }else<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $domain_url = substr($capture_url, 0,strpos($capture_url, '/',8)+1);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $img_url=$domain_url.$img_url;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $pathinfo = pathinfo($img_url); //获取图片路径信息<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $pic_name=$pathinfo['basename']; //获取图片的名字<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if (file_exists($this->save_path.$pic_name)) //如果图片存在,证明已经被抓取过,退出函数<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> echo $img_url . '该图片已经抓取过!
';<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //将图片内容读入一个字符串<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $img_data = @file_get_contents($img_url); //屏蔽掉因为图片地址无法读取导致的warning错误<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if ( strlen($img_data) > $this->img_size ) //下载size比限制大的图片<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> $img_size = file_put_contents($this->save_path . $pic_name, $img_data);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> if ($img_size)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> echo $img_url . '图片保存成功!
';<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> } else<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> echo $img_url . '图片保存失败!
';<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> } else<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> echo $img_url . '图片读取失败!
';<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />} // END<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></p>
来看看一个完整的功能类,直接保存,引用就可以了
<p><p style="padding: 0.5em;max-width: 100%;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);margin-left: 8px;margin-right: 8px;box-sizing: border-box !important;word-wrap: normal !important;display: block !important;word-break: normal !important;overflow: auto !important;">