PHP to crawl the site pictures and save

Source: Internet
Author: User
PHP Remote Crawl site pictures and save

Previously saw online others said to write the program crawl Web images, it feels very magical, I thought when I also write a way to crawl pictures!

?

Just these two days nothing, referring to a PHP crawl image code on the web, focusing on the matching IMG tag and its src attribute of the regular notation,

Encapsulated a PHP remote grab the picture class, tested, the speed is still, two minutes from pension eine pause Ini.iteye.com grabbed more than 110 pictures

?

The code is as follows:

 Save_path= $save _path;    $this->img_size= $img _size;      }/** * Recursive download method for fetching the image of the home page and its sub-pages (recursive recursion) * * @param String $capture _url URL to crawl the image *   */Public Function recursive_download_images ($capture _url) {if (!in_array ($capture _url,self:: $a _url_arr))   Did not crawl {self:: $a _url_arr[]= $capture _url;        Count to static array} else//Fetch, Exit function {return directly;  } $this->download_current_page_images ($capture _url);        Download all pictures of the current page [email protected]??                 [Email protected]_get_contents ($capture _url); Matches the a tag href attribute in the previous section of the regular $a _pattern = "|] +href=[' \ "]? ([^ '\"?] +) [' \ ' >]|           U ";                Preg_match_all ($a _pattern, $content, $a _out, preg_set_order);  $tmp _arr=array (); Defines an array that holds the hyperlink address of the current loop to fetch the image, foreach ($a _out as $k + = $v) {/** * remove empty ' from hyperlink ', ' # '    , '/' and duplicate values           * 1: The value of the hyperlink address cannot be equal to the URL of the current fetch page, otherwise it will fall into a dead loop * 2: The hyperlink is ' or ' # ', '/' is also this page, which will also fall into the dead loop, * 3: Sometimes a super-connected The address will be repeated multiple times in a webpage, if not removed, a sub-page will be repeated download) */if ($v [1] &&!in_array ($v [1],self:: $a _url_arr) &A            Mp;&!in_array ($v [1],array (' # ', '/', $capture _url))) {$tmp _arr[]= $v [1]; }} foreach ($tmp _arr as $k = + $v) {//Hyperlink path address if (Strpos ($v            !==false)//If the URL contains http://, you can directly access {$a _url = $v; }else//Otherwise prove to be relative address, need to re-cobble the access address of the hyperlink {$domain _url = substr ($capture _url, 0,strpos ($capture _url,                '/', 8) +1);            $a _url= $domain _url. $v;        } $this->recursive_download_images ($a _url);   }}/** * Download all the pictures under the current page * * @param String $capture _url The address of the webpage used to crawl the picture * @return An array of all picture img tag URL addresses on the current page */publicfunction download_current_page_images ($capture _url) {[Email protected]_get_contents ($capture _url); Mask warning Error//match img tag in src attribute? Previous section of regular $img_pattern = "|] +src=[' \ "]? ([^ '\"?] +) [' \ ' >]|           U ";        Preg_match_all ($img _pattern, $content, $img _out, Preg_set_order); $photo _num = count ($img _out); Number of images to match Echo '

'. $capture _url. "Find All". $photo _num. "Picture

"; foreach ($img _out as $k = + $v) {$this->save_one_img ($capture _url, $v [1]); }}/** * How to save a single picture * * @param string $capture _url the address of the Web page used to crawl the picture * @param string $img _url need to be guaranteed The URL of the saved picture * */Public Function save_one_img ($capture _url, $img _url) {//Picture path address if (str POS ($img _url, ' http://')!==false) {//$img _url = $img _url; }else {$domain _url = substr ($capture _url, 0,strpos ($capture _url, '/', 8) +1); $img _url= $domain _url. $img _url; } $pathinfo = PathInfo ($img _url); Get Picture path information $pic _name= $pathinfo [' basename ']; Gets the name of the picture if (File_exists ($this->save_path. $pic _name))//If the picture exists, the proof has been crawled, the Exit function {echo $img _url . 'The picture has been crawled!
'; Return }//Read the picture contents into a string $img _data = @file_get_contents ($img _url); Mask off because the image address cannot be read caused by warning error if (strlen ($img _data) > $this->img_size)//download size larger than limit picture { $img _size = file_put_contents ($this->save_path. $pic _name, $img _data); if ($img _size) {echo $img _url. 'picture saved successfully!
'; } else {echo $img _url. 'picture save failed!
'; }} else {echo $img _url. 'picture Read failed!
'; }}}//Endset_time_limit (120); Set the maximum execution time of the script according to the situation $download _img=new download_image (' e:/images/', 0); Instantiate the downloaded Picture object $download_img->recursive_download_images (' http://ini.iteye.com/'); Recursive capture image Method//$download _img->download_current_page_images ($_post[' Capture_url '); Only fetch the current page picture method?>

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.