This article mainly introduces php implementation code for capturing and saving website images, webpage source code capturing, and obtaining image links.
This article mainly introduces php implementation code for capturing and saving website images, webpage source code capturing, and obtaining image links.
This program allows you to capture the source code of a webpage, obtain and analyze image links, and combine the same image links to capture images. Use php's powerful network content processing function to capture all images on a specified website and save them to the current directory. The following code is used:
<? Php/* Complete webpage 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 a regular expression to obtain the image 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." http://www.jb51.net/ ". $ Site_item;} return $ return_list;}/* Get the image name and save it in the specified position */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 () {/* webpage address of the image to be crawled */$ site_name =" http://www.jb51.net/ Sheying/391528.html "; $ img_url = get_img_url ($ site_name); $ scheme = revise_site ($ img_url, $ site_name); $ img_url_unique = array_unique ($ scheme ); // unique array get_pic_file ($ img_url_unique ,". /") ;}main () ;?>
This program also needs to be improved. If the images are in different directories on the website server but the file names are the same, the images may be different at this time, but at the end of saving, the subsequent image will overwrite the previously saved image. The solution is to retrieve whether the file name already exists in the current directory before each save, if yes, rename the image to be saved.
The above is the method for capturing and saving website images in php shared with you, and the bug fixes in practice. I hope this article will be helpful for your learning.