PHP web page capture-php Tutorial

Source: Internet
Author: User
PHP web page capture

  1. Header ('content-Type: text/html; charset = utf-8 ');
  2. /**
  3. * A class used to capture images
  4. */
  5. Class download_image {
  6. Public $ _ save_path = NULL; // save path strength of the image
  7. Public $ _ limit_size = NULL; // the maximum image size
  8. Public static $ _ img_url_old = array (); // store the URL of the captured image
  9. Public static $ _ a_page_url = array (); // stores captured pages
  10. Public function _ construct ($ _ save_path, $ _ limit_size ){
  11. $ This-> _ save_path =$ _ save_path;
  12. $ This-> _ limit_size = $ _ limit_size;
  13. }
  14. Public function get_all_page_image ($ site_url ){
  15. If ($ site_url = ''){
  16. Return false;
  17. }
  18. If (! In_array ($ site_url, self: $ _ a_page_url) {// determines whether the current page has been crawled.
  19. Self: $ _ a_page_url [] = $ site_url; // Save the hyperlink to a static array
  20. } Else {
  21. Return; // jump out if it has been captured
  22. }
  23. $ This-> download_the_page_image ($ site_url );
  24. $ Content = @ file_get_contents ($ site_url );
  25. $ A_page_url = "|] + href = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
  26. $ All_url = array ();
  27. Preg_match_all ($ a_page_url, $ content, $ all_url, PREG_SET_ORDER );
  28. If ($ all_url! = NULL ){
  29. Foreach ($ all_url as $ key => $ val ){
  30. /**
  31. * Static hyperlinks prevent endless loops
  32. * Representation of the link on the current page ('','#','/')
  33. */
  34. If (trim ($ val [1])! = ''&&! In_array ($ val [1], self: $ _ a_page_url )&&! In_array ($ val [1], array ('#', '/', $ site_url ))){
  35. Self: $ _ a_page_url [] = $ val [1]; // write the required hyperlink to the static array.
  36. }
  37. }
  38. }
  39. If (self: $ _ a_page_url! = NULL ){
  40. Foreach (self ::$ _ a_page_url as $ keys => $ vals ){
  41. If (strpos ($ vals, 'http: // ') = false) {// when the hyperlink does not contain http: //, you cannot directly access
  42. // When the image link address is a relative address, it is a patchwork address.
  43. $ A_domain_url = substr ($ site_url, 0, strpos ($ site_url, '/', 8) + 1 );
  44. $ A_img_url = $ a_domain_url. $ vals;
  45. }
  46. // Recursive call to access every hyperlink page
  47. $ This-> get_all_page_image ($ a_img_url );
  48. }
  49. }
  50. }
  51. /**
  52. * Download all image links on the current page
  53. * @ Param $ site_url <页面地址>
  54. */
  55. Public function download_the_page_image ($ site_url ){
  56. // Obtain all content on the current link page
  57. $ Img_pattern = NULL;
  58. $ Content = @ file_get_contents ($ site_url );
  59. $ Img_pattern = "|] + src = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
  60. // Globally match all image links
  61. $ Img_out = array ();
  62. Preg_match_all ($ img_pattern, $ content, $ img_out, PREG_SET_ORDER );
  63. Echo ''. $ site_url. 'found'. count ($ img_out). 'image ';
  64. // Print_r ($ img_out [1]);
  65. Foreach ($ img_out as $ key => $ val ){
  66. // Echo htmlspecialchars ($ val [1]).'
    ';
  67. $ This-> save_one_image ($ site_url, $ val [1]);
  68. }
  69. }
  70. Public function save_one_image ($ site_url, $ img_url ){
  71. If (strpos ($ img_url, 'http: // ') === false ){
  72. // When the image link address is a relative address, it is a patchwork address.
  73. $ Domain_url = substr ($ site_url, 0, strpos ($ site_url, '/', 8) + 1 );
  74. $ Img_url = $ domain_url. $ img_url;
  75. }
  76. $ Pic_name = basename ($ img_url); // Obtain the image name
  77. If (in_array ($ img_url, self: $ _ img_url_old )){
  78. Echo $ img_url. 'the image has been captured!
    ';
  79. Return;
  80. }
  81. // Get the image content and write a string
  82. $ Img_data = @ file_get_contents ($ img_url );
  83. If (strlen ($ img_data) <$ this-> _ limit_size) {// The image size is within the limit.
  84. $ Img_boo = @ file_put_contents ($ this-> _ save_path.md5 (microtime (). $ pic_name, $ img_data );
  85. If ($ img_boo ){
  86. Echo $ img_url. 'the image is saved successfully!
    ';
  87. Self: $ _ img_url_old [] = $ img_url;
  88. } Else {
  89. Echo $ img_url. 'an error occurred while saving the image!
    ';
  90. }
  91. } Else {
  92. Echo $ img_url. 'the image size is out of the limit!
    ';
  93. }
  94. }
  95. }
  96. Set_time_limit (0 );
  97. $ Download_images = new download_image ('surces _ Img/', 1024*1024*100 );
  98. $ Download_images-> get_all_page_image ('http: // www.baidu.com /');
  99. ?>


Screenshot, PHP

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.