Guest User

Untitled

a guest
May 30th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. <?php
  2.  
  3. // It may take a whils to crawl a site ...
  4. set_time_limit(10000);
  5.  
  6. // Inculde the phpcrawl-mainclass
  7. include("PHPCrawl/libs/PHPCrawler.class.php");
  8.  
  9. // Extend the class and override the handleDocumentInfo()-method
  10. class MyCrawler extends PHPCrawler
  11. {
  12.   function handleDocumentInfo($DocInfo)
  13.   {
  14.     // Just detect linebreak for output ("\n" in CLI-mode, otherwise "<br>").
  15.     if (PHP_SAPI == "cli") $lb = "\n";
  16.     else $lb = "<br />";
  17.  
  18.     // Print the URL and the HTTP-status-Code
  19.     echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;
  20.     if($DocInfo->http_status_code==505) {
  21.         echo "Error_code: ".$DocInfo->error_code.$lb."Occured: ".$DocInfo->error_occured.$lb."String: ".$DocInfo->error_string.$lb;
  22.         echo $DocInfo->header_send.$lb;
  23.     }
  24.     echo "File: ".$DocInfo->file.$lb;
  25.     echo "C: ".$DocInfo->content_type." (size: ".strlen($DocInfo->content).").".$lb;
  26.    
  27.     // Print if the content of the document was be recieved or not
  28.    
  29.     // Now you should do something with the content of the actual
  30.     // received page or file ($DocInfo->source), we skip it in this example
  31.    
  32.     echo $lb;
  33.    
  34.     flush();
  35.   }
  36. }
  37.  
  38. // Now, create a instance of your class, define the behaviour
  39. // of the crawler (see class-reference for more options and details)
  40. // and start the crawling-process.
  41.  
  42. $crawler = new MyCrawler();
  43.  
  44. // URL to crawl
  45. $crawler->setURL("http://www.lidingo.se/toppmeny/stadpolitik/handlingarochprotokoll/sammantraden.html");
  46.  
  47. // Only receive content of files with content-type "text/html"
  48. $crawler->addContentTypeReceiveRule("#text/html#");
  49. $crawler->addContentTypeReceiveRule("#application/pdf#");
  50.  
  51. $crawler->addURLFollowRule("#handlingarochprotokoll|lexportlet#");
  52.  
  53. // Ignore links to pictures, dont even request pictures
  54. $crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$|print# i");
  55.  
  56. // Store and send cookie-data like a browser does
  57. $crawler->enableCookieHandling(true);
  58.  
  59. $crawler->setPageLimit(400);
  60.  
  61.  
  62. // Set the traffic-limit to 1 MB (in bytes,
  63. // for testing we dont want to "suck" the whole site)
  64. //$crawler->setTrafficLimit(1000 * 1024);
  65.  
  66. // Thats enough, now here we go
  67. $crawler->go();
  68.  
  69. // At the end, after the process is finished, we print a short
  70. // report (see method getProcessReport() for more information)
  71. $report = $crawler->getProcessReport();
  72.  
  73. if (PHP_SAPI == "cli") $lb = "\n";
  74. else $lb = "<br />";
  75.    
  76. echo "Summary:".$lb;
  77. echo "Links followed: ".$report->links_followed.$lb;
  78. echo "Documents received: ".$report->files_received.$lb;
  79. echo "Bytes received: ".$report->bytes_received." bytes".$lb;
  80. echo "Process runtime: ".$report->process_runtime." sec".$lb;
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment