PHP regular grabs images under the entire domain name

WBOY
Release: 2016-07-25 08:50:05
Original
969 people have browsed it
Code source: jUnion

Applicable platforms: Windows, Linux (Ubuntu), php-5.2.5+, Apache

Function: Capture images of the entire site. There is no curl plug-in development with PHP yet. Later improvement

Configuration: Under the config directory
domain_name: domain name (default: bizhibar.com)
request_site: website URL (default: http://www.bizhibar.com/)
request_url: which page of the website to start from (default: http://www .bizhibar.com/)
accept_type: picture type (default: gif, bmp, png, ico, jpg, jpeg)
save_path: picture saving path (default: savefiles/)
partition_name: picture saving directory name prefix (default: img_ )
dir_file_limit: How many files each directory allows (default: 100)
serialize_img_size: How many image addresses are read before caching them into the accompImg file in the cache directory. These addresses will be ignored the next time you continue to crawl. (Default: 30)
serialize_url_size: Same as serialize_url_size, how many link addresses have been read before caching to the overURL under the cache directory
. These addresses will be ignored the next time you continue to crawl. (Default: 10)

Note: I welcome your criticism and advice. If you have any new questions or areas that need improvement, please give me feedback
  1. set_time_limit(0);
  2. require dirname(__FILE__).DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'Capture.const.php';
  3. require __Home__.'include'.__Os__.'Capture. class.php';
  4. $_cfg = array(
  5. 'site' => __Home__.'config'.__Os__.'capture.site.php',
  6. 'preg' => __Home__.'config'.__Os__. 'capture.preg.php',
  7. 'accompImg' => __Home__.'cache'.__Os__.'accompImg',
  8. 'overURL' => __Home__.'cache'.__Os__.'overURL'
  9. );
  10. $_parse = new Capture( $_cfg );
  11. $_parse->parseQuestUrl();
  12. ?>
Copy code
  1. /**
  2. * The main class
  3. * @author pankai<530911044@qq.com>
  4. * @date 2013-08-10
  5. */
  6. class Capture {
  7. private static $_Config = array();
  8. private static $_CapSite = NULL;
  9. private static $_CapPreg = NULL;
  10. private static $_overURL = array();
  11. private $_mark = FALSE;
  12. private static $_markTime = 1;
  13. /**
  14. * initialize the main class: Capture
  15. * @param $_cfg array
  16. */
  17. public function __construct( &$_cfg ) {
  18. self::$_Config = &$_cfg;
  19. self::$_CapSite = require $_cfg['site'];
  20. self::$_CapPreg = require $_cfg['preg'];
  21. foreach( self::$_CapPreg as $_key => $_value ) {
  22. self::$_CapPreg[$_key] = str_replace( '_request_site', self::$_CapSite['request_site'], $_value );
  23. }
  24. self::import( 'file.OperateFile' );
  25. if( file_exists( $_cfg['overURL'] ) && filesize( $_cfg['overURL'] ) > 0 ) {
  26. $_contents = OperateFile::readText( $_cfg['overURL'], filesize( $_cfg['overURL'] ) );
  27. self::$_overURL = unserialize( $_contents );
  28. }
  29. self::import('pivotal.Pivotal');
  30. if( file_exists( $_cfg['accompImg'] ) && filesize( $_cfg['accompImg'] ) > 0 ) {
  31. $_contents = OperateFile::readText( $_cfg['accompImg'], filesize( $_cfg['accompImg'] ) );
  32. Pivotal::$_accompImg = unserialize( $_contents );
  33. }
  34. }
  35. /**
  36. * load class, follow Java pragrammer(package): import com.jUnion.Capture
  37. * @param $_class
  38. */
  39. public static function import( $_class ) {
  40. require_once __Home__.'include'.__Os__.str_replace( '.', __Os__, $_class ).'.class.php';
  41. }
  42. /**
  43. * create an instance of Pivotal class
  44. * @param $_source
  45. */
  46. private function getCapInstance( &$_source ) {
  47. $this->_mark = FALSE;
  48. $_Captal = new Pivotal( self::$_Config, $_source );
  49. $_tagA = $_Captal->parseUrl();
  50. $this->_mark = TRUE;
  51. return $_tagA;
  52. }
  53. /**
  54. * go forward one by one
  55. * @param $_tagArr
  56. */
  57. private function roundTagA( &$_tagArr ) {
  58. if( $_tagArr == NULL ) {
  59. return;
  60. }
  61. $_tagArrLength = count( $_tagArr );
  62. for( $i = 0; $i < $_tagArrLength; $i ++ ) {
  63. if( is_array( $_tagArr[ $i ] ) ) {
  64. $this->roundTagA( $_tagArr[ $i ] );
  65. }
  66. else {
  67. if( stripos( $_tagArr[$i], self::$_CapSite['domain_name'] )
  68. === FALSE ) {
  69. continue;
  70. }
  71. if( in_array( $_tagArr[$i], self::$_overURL ) ) {
  72. continue;
  73. }
  74. self::$_overURL[] = $_tagArr[$i];
  75. if( count( self::$_overURL ) % self::$_CapSite['serialize_url_size'] == 0 ) {
  76. OperateFile::setText( self::$_Config['overURL'], serialize( self::$_overURL ) );
  77. }
  78. do {
  79. $_tagA = $this->getCapInstance( Http::get( $_tagArr[$i] ) );
  80. sleep( self::$_CapSite['preform_page_time'] * self::$_markTime );
  81. if( $this->_mark === TRUE ) {
  82. self::$_markTime = self::$_CapSite['preform_page_time'];
  83. break;
  84. }
  85. self::$_markTime *= 2;
  86. } while( true );
  87. /* parse the main page and return next page */
  88. $this->roundTagA( $_tagA );
  89. }
  90. }
  91. }
  92. public function parseQuestUrl() {
  93. self::import('http.Http');
  94. $_round_Arr = $this->getCapInstance( Http::get( self::$_CapSite['request_url'] ) );
  95. $this->roundTagA( $_round_Arr );
  96. }
  97. }
  98. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!