This article to share with you about the PHP crawl site image and save the code, this example detailed description of the capture and save the image to the local method, the need for friends can refer to
Example, PHP crawls the code of the website data.
code example:
<?php/** * A class for capturing pictures * * @package default * @author Wujunwei */class Download_image {public $s Ave_path; Capture the image's save address//Crawl image size limit (in bytes) only the size of the picture than the larger limit of public $img _size=0; Defines a static array that is used to record the hyperlinked addresses that have been crawled, to avoid repeatedly fetching public static $a _url_arr=array (); /** * @param String $save The Save address of the _path grab image * @param Int $img The Save address of the _size grab image */Public function __construct ($save _path, $img _size) {$this->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 grab the image * */Public Function recursive_download_images ($capture _url) {if (!in_array ($capture _url,self: : $a _url_arr))//Not crawled {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//mask off with @ Warning error caused by fetch address unreadable $content = @file_get_contents ($capture _url); Match the A-tag href attribute in the previous section of the regular $a _pattern = "|<a[^>]+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) {/** * Removes the hyperlink from the Null ', ' # ', '/' and duplicate value * 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 hyperlink address will appear repeatedly in a Web page, if not removed, a sub-page will be repeated download) */if ($v [1] &&!in_array ($ V[1],self:: $a _url_arr) &&!in_array ($v [1],array (' # ', '/', $capture _url)]) {$tmp _arr []= $v [1]; }} foreach ($tmp _arr as $k = $v) {//Hyperlink path address if (Strpos ($v, '/http ')!==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 _u RL, '/', 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 page address used to crawl the image * @return Array current An array of all picture img tag URL addresses on the page */Public Function download_current_page_images ($capture _url) {$content =@f Ile_get_contents ($capture _url); Mask warning Error//match img tag in src attribute? Previous part of the 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 ' <h1> '. $capture _url. "Find All". $photo _num. "Photo
Related recommendations:
Grab a picture from a Web site and download it automatically to a folder
How to implement PHP crawl images _php Tutorial
Grab a picture using PHP's Snoopy class _php tutorial