PHP采集系统

浏览量:666 | 分类:PHP | 发布日期:2009-08-13

今天公司PHP牛人教了PHP采集系统的原理^_^,太牛了!

代码如下
  1. <?php
  2.  
  3. //获得网页内容
  4.     function getFileContents($url) {
  5.         $user_agent="User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Windows 2000; Windows XP)";
  6.         $urlparts = parse_url($url);
  7.         $path = $urlparts['path'];
  8.         $host = $urlparts['host'];
  9.         if (!empty($urlparts['query']))
  10.         $path .= "?".$urlparts['query'];
  11.         if (isset ($urlparts['port'])) {
  12.             $port = (int) $urlparts['port'];
  13.         } else
  14.         if ($urlparts['scheme'] == "http") {
  15.             $port = 80;
  16.         } else
  17.         if ($urlparts['scheme'] == "https") {
  18.             $port = 443;
  19.         }
  20.  
  21.         if ($port == 80) {
  22.             $portq = "";
  23.         } else {
  24.             $portq = ":$port";
  25.         }
  26.  
  27.         $all = "*/*";
  28.  
  29.         $request = "GET $path HTTP/1.0rnHost: $host$portqrnAccept: $allrnAccept-Encoding: identityrnUser-Agent: $user_agentrnrn";
  30.  
  31.         $fsocket_timeout = 60;
  32.         if (substr($url, 0, 5) == "https") {
  33.             $target = "ssl://".$host;
  34.         } else {
  35.             $target = $host;
  36.         }
  37.  
  38.  
  39.         $errno = 0;
  40.         $errstr = "";
  41.         $fp = @ fsockopen($target, $port, $errno, $errstr, $fsocket_timeout);
  42.         if (!$fp) {
  43.             $contents['state'] = "NOHOST";
  44.             print "Error: $errstr";
  45.             return $contents;
  46.         } else {
  47.             if (!fputs($fp, $request)) {
  48.                 $contents['state'] = "Cannot send request";
  49.                 return $contents;
  50.             }
  51.             $data = null;
  52.             socket_set_timeout($fp, $fsocket_timeout);
  53.             $status = socket_get_status($fp);
  54.             while (!feof($fp) && !$status['timed_out']) {
  55.                 $data .= fgets($fp, 8192);
  56.             }
  57.             fclose($fp);
  58.             if ($status['timed_out'] == 1) {
  59.                 $contents['state'] = "timeout";
  60.             } else{
  61.                 if(strstr($data,"Location: ")&&strstr($data,"Cache-Control: private")){
  62.                     $contents['state'] = "jump";
  63.                     $contents['file'] = substr($data, strpos($data, "rnrn") + 4);
  64.                 }
  65.                 else{
  66.                     $contents['state'] = "ok";
  67.                     $contents['file'] = substr($data, strpos($data, "rnrn") + 4);
  68.                 }
  69.             }
  70.         }
  71.  
  72.         return $contents;
  73.     }
  74.  
  75.     /*
  76.     检查url文件是否可以读取
  77.     check if file is available and in readable form
  78.     */
  79.     function url_status($url) {
  80.         $user_agent="User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Windows 2000; Windows XP)";
  81.         $urlparts = parse_url($url);
  82.         $path = $urlparts['path'];
  83.         $host = $urlparts['host'];
  84.         if (!empty($urlparts['query']))
  85.         $path .= "?".$urlparts['query'];
  86.  
  87.         if (isset ($urlparts['port'])) {
  88.             $port = (int) $urlparts['port'];
  89.         } else
  90.         if ($urlparts['scheme'] == "http") {
  91.             $port = 80;
  92.         } else
  93.         if ($urlparts['scheme'] == "https") {
  94.             $port = 443;
  95.         }
  96.  
  97.         if ($port == 80) {
  98.             $portq = "";
  99.         } else {
  100.             $portq = ":$port";
  101.         }
  102.  
  103.         $all = "*/*"; //just to prevent "comment effect" in get accept
  104.         $request = "HEAD $path HTTP/1.1rnHost: $host$portqrnAccept: $allrnAccept-Charset: iso-8859-1rnAccept-Encoding: identityrnUser-Agent: $user_agentrnrn";
  105.  
  106.         if (substr($url, 0, 5) == "https") {
  107.             $target = "ssl://".$host;
  108.         } else {
  109.             $target = $host;
  110.         }
  111.  
  112.         $fsocket_timeout = 60;
  113.         $errno = 0;
  114.         $errstr = "";
  115.         $fp = fsockopen($target, $port, $errno, $errstr, $fsocket_timeout);
  116.  
  117.         $linkstate = "ok";
  118.         if (!$fp) {
  119.             $status['state'] = "NOHOST";
  120.         } else {
  121.             socket_set_timeout($fp, $fsocket_timeout);
  122.             fputs($fp, $request);
  123.             $answer = fgets($fp, 4096);
  124.             $regs = Array ();
  125.             if (ereg("HTTP/[0-9.]+ (([0-9])[0-9]{2})", $answer, $regs)) {
  126.                 $httpcode = $regs[2];
  127.                 $full_httpcode = $regs[1];
  128.  
  129.                 if ($httpcode <> 2 && $httpcode <> 3) {
  130.                     $status['state'] = "Unreachable: http $full_httpcode";
  131.                     $linkstate = "Unreachable";
  132.                 }
  133.             }
  134.  
  135.             if ($linkstate <> "Unreachable") {
  136.                 while ($answer) {
  137.                     $answer = fgets($fp, 4096);
  138.  
  139.                     if (ereg("Location: *([^nr ]+)", $answer, $regs) && $httpcode == 3 && $full_httpcode != 302) {
  140.                         $status['path'] = $regs[1];
  141.                         $status['state'] = "Relocation: http $full_httpcode";
  142.                         fclose($fp);
  143.                         return $status;
  144.                     }
  145.  
  146.                     if (eregi("Last-Modified: *([a-z0-9,: ]+)", $answer, $regs)) {
  147.                         $status['date'] = $regs[1];
  148.                     }
  149.  
  150.                     if (eregi("Content-Type:", $answer)) {
  151.                         $content = $answer;
  152.                         $answer = '';
  153.                         break;
  154.                     }
  155.                 }
  156.                 $socket_status = socket_get_status($fp);
  157.                 if (eregi("Content-Type: *([a-z/]*)", $content, $regs)) {
  158.                     if ($regs[1] == 'text/html' || $regs[1] == 'text/' || $regs[1] == 'text/plain') {
  159.                         $status['content'] = 'text';
  160.                         $status['state'] = 'ok';
  161.                     } else if ($regs[1] == 'application/pdf') {
  162.                         $status['content'] = 'pdf';
  163.                         $status['state'] = 'ok';
  164.                     } else if ($regs[1] == 'application/msword') {
  165.                         $status['content'] = 'doc';
  166.                         $status['state'] = 'ok';
  167.                     } else {
  168.                         $status['state'] = "Not text or html";
  169.                     }
  170.  
  171.                 } else
  172.                 if ($socket_status['timed_out'] == 1) {
  173.                     $status['state'] = "Timed out (no reply from server)";
  174.  
  175.                 } else
  176.                 $status['state'] = "Not text or html";
  177.  
  178.             }
  179.         }
  180.         fclose($fp);
  181.         return $status;
  182.     }
  183.  
  184.  
  185.     $host = 'http://www.admin5.com';
  186.     $list_exp = '<div class="itembox"';
  187.     $url_start = '<a href="';
  188.     $url_end = '" target=';
  189.     $detail_title_start = '<h1>';
  190.     $detail_title_end = '</h1>';
  191.     $detail_summary_start = '<div id="arctext">';
  192.     $detail_summary_end = '<div id="arctext">';
  193.  
  194.     $max_page = 179;
  195.     for($page=$max_page;$page>0;$page--){
  196.  
  197.         $url = "http://www.admin5.com/browse/26/list_".$page.".shtml";
  198.  
  199.         $status = url_status($url);
  200.  
  201.         if($status['content'] == 'text' && $status['state'] == 'ok'){
  202.  
  203.             $files = getFileContents($url);
  204.  
  205.             $contents = $files['file'];
  206.  
  207.             $arr = explode($list_exp, $contents);
  208.  
  209.             for($i=1;$i<count($arr);$i++){
  210.                 $detail_url = "";
  211.                 $detail_url = strstr($arr[$i], $url_start);
  212.                 $detail_url = str_replace($url_start, "", $detail_url);
  213.                 $pos = strpos($detail_url, $url_end);
  214.                 $detail_url = substr($detail_url, 0, $pos);
  215.                 $detail_url = $host.$detail_url;
  216.  
  217.                 $summary = getFileContents($detail_url);
  218.  
  219.                 print_r($summary);
  220.                 exit;
  221.             }
  222.  
  223.         }
  224.  
  225.     }
  226.  
  227. ?>

上一篇: 仙剑三之紫萱与长卿忘情湖分别

下一篇: 心情

评论