PHP Fetch and save the implementation code of the website picture, _php tutorial

Source: Internet
Author: User
Tags fread

PHP crawls and saves the implementation code of the website image,


This program realizes the Web page source code capture, the image link obtains, the analysis, and the same picture link merging function, realizes the picture grasping function. Take advantage of PHP's powerful Web content processing function to fetch all the pictures on the specified Web site and save them in the current directory, with the following code:

<?php/* Complete Web content capture function */function Get_img_url ($site _name) {$site _fd = fopen ($site _name, "R");  $site _content = "";  while (!feof ($site _fd)) {$site _content. = Fread ($site _fd, 1024);  }/* Use regular expressions to get a picture link */$reg _tag = '//';  $ret = Preg_match_all ($reg _tag, $site _content, $match _result);  Fclose ($site _fd); return $match _result[1]; }/* Modify the image link */function revise_site ($site _list, $base _site) {foreach ($site _list as $site _item) {if (Preg_match ('/^   http/', $site _item)) {$return _list[] = $site _item; }else{$return _list[] = $base _site. "  /". $site _item; }} return $return _list; }/* Get the name of the picture and save it at the specified location */function Get_pic_file ($pic _url_array, $pos) {$reg _tag = '/.*\/(. *?)  $/';  $count = 0;   foreach ($pic _url_array as $pic _item) {$ret = Preg_match_all ($reg _tag, $pic _item, $t _pic_name);   $pic _name = $pos. $t _pic_name[1][0];  $pic _url = $pic _item; Print ("Downloading". $pic _url. "   ");   $img _read_fd = fopen ($pic _url, "R");   $img _write_fd = fopen ($pic _name, "w");  $img _content = ""; while (!feof ($img _read_fd)) {$img _content. = fread ($img _read_fd,1024);   } fwrite ($img _write_fd, $img _content);   Fclose ($img _read_fd);   Fclose ($img _write_fd);  Print ("[OK]"); } return 0;  } function Main () {/* The page address of the image to be crawled */$site _name = "http://www.bkjia.com/sheying/391528.html";  $img _url = Get_img_url ($site _name);  $img _url_revised = Revise_site ($img _url, $site _name); $img _url_unique = Array_unique ($img _url_revised); Unique array get_pic_file ($img _url_unique, "./");  } main ();?>

This program still needs to be perfected, if the picture on the site server on the different directory but the file name is the same, at this time the picture may be not the same, but in the last save, the picture will be saved in the previous picture will be overwritten, the workaround is to retrieve the name of the file in the current directory before each save, and then rename the picture you want to save.

The above is for everyone to share the method of PHP crawl and save the site pictures, as well as in the practice of correcting the process is not perfect place, I hope this article on everyone's learning to help.

http://www.bkjia.com/PHPjc/1065574.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065574.html techarticle PHP Crawl and save the Web site image implementation code, this program realizes the Web page source code capture, Image link acquisition, analysis, and the same image link merging function, to achieve the picture capture ...

  • Related Article

    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.