Download the autohome forum post for offline browsing (PHP version)

Source: Internet
Author: User

I posted a post titled sending an original tool and downloading the autohome forum post for offline browsing, written in C # winform. Now I have a PHP version.

The analysis process is the same as the previous post, but PHPCodeTo achieve this,I added comments to the key points.

 1 <? PHP  2   Set_time_limit (0 );// Prevent timeout  3   /* Longware@gmail.com */  4   Function Gethttpcontentbysock ( $ URL ,$ Referer = "" ){// Obtain the HTTP request content, including HTML and binary images.  5       $ Info = Parse_url ( $ URL  );  6       $ Referer = Empty ( $ Referer )? $ URL : $ Referer  ; 7       $ Contents ='' ;  8   9       $ Fp = Fsockopen ( $ Info ['Host'], 80, $ Errno , $ Errstr , 30 );  10       If (! $ Fp  ){ 11           Echo " $ Errstr ( $ Errno ) <Br/> \ n" ;  12 } Else  {  13           $ Out = "Get $ URL HTTP/1.1 \ r \ n" ;  14           $ Out . = "Host :". $ Info ['Host']. "\ r \ n" ;  15           $ Out . = "Connection: keep-alive \ r \ n" ;  16           $ Out . = "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) \ r \ n" ;  17           $ Out . = "Accept: */* \ r \ n" ;  18           $ Out . = "Referer: $ Referer \ R \ n";// This cannot be less  19           $ Out . = "Accept-language: ZH-CN, ZH; q = 0.8 \ r \ n" ;  20           $ Out . = "Accept-charset: GBK, UTF-8; q = 0.7, *; q = 0.3 \ r \ n" ;  21   22           Fwrite ( $ Fp , $ Out  );  23  24           $ Line = 1; $ Flag = False  ;  25           While (! Feof ( $ Fp  )){  26               $ TMP = Fgets ( $ Fp , 2048 );  27              If ( $ Line > 1 ){  28                   If ( $ Flag  ){  29                       $ Contents . = $ TMP  ;  30 } Else  {  31                       If (Strlen ( Trim ( $ TMP ) <1 ){// Remove the HTTP Response Header text  32                           $ Flag = True  ;  33   }  34   }  35   }  36              $ Line ++ ;  37   }  38           Fclose ( $ Fp  );  39   }  40   41       Return   $ Contents  ;  42   } 43   44   /* By longware */  45   Function Writecontent ( $ URL , $ Page  ){// Write an object  46       $ Html = Gethttpcontentbysock ( $ URL , $ URL  );  47   48      $ Pattern = "/Src9 = \" http: \/[a-zA-Z0-9 _ \. \/] + \"/" ;// RegEx matches the image URL  49       Preg_match_all ( $ Pattern , $ Html , $ Matches , Preg_pattern_order );  50       //  Print_r ($ matches );  51   52       $ Search = Array ('"', 'Src9 =' );  53       Foreach ( $ Matches   As   $ Matche_array  ){// Obtain the real image URL  54           Foreach ( $ Matche_array   As   $ Matche_line  ){  55              $ Imgurl = Str_replace ( $ Search ,'', $ Matche_line  );  56               $ Path_info = Explode ("//", $ Imgurl  );  57               $ Dir = "./". Dirname ( $ Path_info [1]). "/";// Create a directory based on the URL  58               If (! Is_dir ( $ Dir  )){  59                   Mkdir ( $ Dir , 0777, True  );  60   }  61               $ Filename = "./".$ Path_info [1 ];  62               File_put_contents ( $ Filename , Gethttpcontentbysock ( $ Imgurl , $ URL  ));// Get the image and write the file  63               Echo   $ Filename . "<Br> \ r \ n" ;  64   } 65   }  66   67       //  Replace href  Replace useless code  68       $ Html = Str_replace ('Src = "http://x.autoimg.cn/club/lazyload.png" src9 = "', 'src = "', $ Html  );  69       $ Html = Str_replace ('HTTP ://','./', $ Html  );  70       $ Html = Str_replace ('Onload = ', 'x1 = ', $ Html  );  71       $ Html = Str_replace ('Onerror = ', 'x2 = ', $ Html  );  72       $ Html = Str_replace ('<Script',' <a style = "display: none "', $ Html  );  73       $ Html = Str_replace ('</Script',' </', $ Html  );  74   75       $ Filename = "./Threadowner-o-200042-19582947 -". $ Page . ". Html "; //  Threadowner-o-200042-19582947-2.html Write a local HTML file  76       File_put_contents ( $ Filename , $ Html  );  77       Echo   $ Filename . "------------------------------------------ <Br> \ r \ n" ;  78   }  79   /* By longware */  80  //  Main  81   If ( Empty ( $ _ Server ['Argv'] [1]) | Empty ( $ _ Server ['Argv'] [2]) { Die ('Input ARGs' );}  82   $ URL = "Http://club.autohome.com.cn/bbs/threadowner-o-200042-19582947-#.html" ;// Main Post URL rule 83   $ Min = $ _ Server ['Argv'] [1 ];// Download the start page  84   $ Max = $ _ Server ['Argv'] [2 ];// Download the end page  85   //  Print_r ($ _ server ['argv']);  86   For ($ I = $ Min ; $ I <= $ Max ; $ I ++ ){  87 Writecontent ( Str_replace ('#', $ I , $ URL ), $ I  );  88   }  89  /* By longware */  90 ?>

Save the preceding codeBBS. phpAnd then createDown. cmdFile, the content is as follows

 
D: \ webserver \ PHP \ php.exe D: \ wwwroot \ autohome \ bbs. php1 100

Double-clickDown. CMD file, Let him download it slowly.

If you want to access$ MinAnd$ MaxChange$ _ Request ['Min']And$ _ Request ['Max']After allCMDThe method is a little faster than the browser.

However, PHP is much less efficient than winform, because PHP does not have a ready-made multi-thread.

 

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.