|
Example: php code for capturing website data.
- /**
- * A category used to capture images
- *
- * @ Package default
- * @ Author WuJunwei
- */
- Class download_image
- {
- Public $ save_path; // save the image capture address
- // The size limit of the captured image (unit: Byte) is greater than the size limit.
- Public $ img_size = 0;
- // Define a static array to record the URL of a previously crawled hyperlink to avoid repeated capturing
- Public static $ a_url_arr = array ();
- /**
- * @ Param String $ save_path: address for saving the captured image
- * @ Param Int $ img_size: address for saving the captured image
- */
- Public function _ construct ($ save_path, $ img_size)
- {
- $ This-> save_path = $ save_path;
- $ This-> img_size = $ img_size;
- }
- /**
- * Recursive method for downloading and capturing pictures on the home page and its subpages (recursive recursion)
- *
- * @ Param String $ capture_url the URL used to capture the image
- *
- */
- Public function recursive_download_images ($ capture_url)
- {
- If (! In_array ($ capture_url, self: $ a_url_arr) // never captured
- {
- Self: $ a_url_arr [] = $ capture_url; // included in the static array
- } Else // crawled and exited the function directly
- {
- Return;
- }
- $ This-> download_current_page_images ($ capture_url); // download all images on the current page
- // Use @ to block the warning error caused by the unreadable captured address.
- $ Content = @ file_get_contents ($ capture_url );
- // Match the href attribute of tag? Previous regular expressions
- $ A_pattern = "|] + href = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
- Preg_match_all ($ a_pattern, $ content, $ a_out, PREG_SET_ORDER );
- $ Tmp_arr = array (); // defines an array to store the hyperlink address for capturing images in the current loop.
- Foreach ($ a_out as $ k => $ v)
- {
- /**
- * Remove the null '', '#', '/' and repeated values in the hyperlink.
- * 1: The value of the hyperlink address cannot be equal to the url of the currently crawled page. Otherwise, the url will be in an endless loop.
- * 2: The hyperlink is ''or '#', and '/' is also the current page, which will also fall into an endless loop,
- * 3: sometimes a hyperlink address appears multiple times on a webpage. if this address is not removed, a subpage is repeatedly downloaded)
- */
- 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)
- {
- // URL of the hyperlink
- If (strpos ($ v, 'http ://')! = False) // if the url contains http: //, you can directly access
- {
- $ A_url = $ v;
- } Else // otherwise, it indicates the relative address. you need to splice the hyperlink's access address.
- {
- $ Domain_url = substr ($ capture_url, 0, strpos ($ capture_url, '/', 8) + 1 );
- $ A_url = $ domain_url. $ v;
- }
- $ This-> recursive_download_images ($ a_url );
- }
- }
- /**
- * Download all images on the current webpage
- *
- * @ Param String $ capture_url the webpage address used to capture images
- * @ Return Array an Array of the URLs of all img tags on the current webpage
- */
- Public function download_current_page_images ($ capture_url)
- {
- $ Content = @ file_get_contents ($ capture_url); // block warning errors
- // Matching the src attribute of the img label? Previous regular expressions
- $ Img_pattern = "|] + src = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
- Preg_match_all ($ img_pattern, $ content, $ img_out, PREG_SET_ORDER );
- $ Photo_num = count ($ img_out );
- // Number of matched images
- Echo ''. $ capture_url." found ". $ photo_num." images ";
- Foreach ($ img_out as $ k => $ v)
- {
- $ This-> save_one_img ($ capture_url, $ v [1]);
- }
- }
- /**
- * How to save a single image
- *
- * @ Param String $ capture_url the webpage address used to capture images
- * @ Param String $ img_url the url of the image to be saved
- *
- */
- Public function save_one_img ($ capture_url, $ img_url)
- {
- // Image path
- If (strpos ($ 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); // Obtain the image path information
- $ Pic_name = $ pathinfo ['basename']; // Obtain the image name.
- If (file_exists ($ this-> save_path. $ pic_name) // if the image exists, it indicates that it has been crawled and exits the function.
- {
- Echo $ img_url. 'the image has been captured!
';
- Return;
- }
- // Read the image content into a string
- $ Img_data = @ file_get_contents ($ img_url); // block the warning error caused by the inability to read the image address.
- If (strlen ($ img_data)> $ this-> img_size) // download an image with a larger size ratio.
- {
- $ Img_size = file_put_contents ($ this-> save_path. $ pic_name, $ img_data );
- If ($ img_size)
- {
- Echo $ img_url. 'the image is saved successfully!
';
- } Else
- {
- Echo $ img_url. 'an error occurred while saving the image!
';
- }
- } Else
- {
- Echo $ img_url. 'reading the image failed!
';
- }
- }
- } // END
- Set_time_limit (120); // sets the maximum execution time of the script based on actual conditions.
- $ Download_img = new download_image ('E:/images/', 0); // instantiate the downloaded image object
- $ Download_img-> recursive_download_images ('http: // bbs.it-home.org/'); // recursive image capturing method
- // $ Download_img-> download_current_page_images ($ _ POST ['capture _ url']); // only capture the image of the current page
- ?>
|