Ver Mensaje Individual
  #8 (permalink)  
Antiguo 28/10/2012, 00:39
Avatar de portalmana
portalmana
 
Fecha de Ingreso: septiembre-2007
Ubicación: Montevideo-Uruguay
Mensajes: 633
Antigüedad: 16 años, 7 meses
Puntos: 80
Respuesta: Evitar utilizar loop recursivo

Código PHP:
Ver original
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3.  
  4. $server_link = mysql_connect("localhost", "root", "");
  5. if(!$server_link) {
  6.     die("Fall&oacute; la Conexi&oacute;n ". mysql_error());
  7. }
  8.  
  9. $db_selected = mysql_select_db("buscadorfulltext", $server_link);
  10. if(!$db_selected) {
  11.     die("No se pudo seleccionar la Base de Datos ". mysql_error());
  12. }
  13.  
  14.  
  15. function storeLink($titulo,$descripcion,$url,$keywords, $prof)
  16. {
  17.     $query = "INSERT INTO spider (webTitulo, webDescripcion, weburl, webkeywords, prof) VALUES ('$titulo', '$descripcion', '$url', '$keywords', $prof)";
  18.     mysql_query($query) or die('Error, falló la inserción de datos');
  19. }
  20.  
  21.  
  22.  
  23. function extraer($url, $prof, $patron)
  24. {
  25.     $userAgent = 'Interredu';
  26.  
  27.     $ch = curl_init();
  28.     curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  29.     curl_setopt($ch, CURLOPT_URL,$url);
  30.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(("Accept-Language: es-es,en")));
  31.     curl_setopt($ch, CURLOPT_FAILONERROR, true);
  32.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  33.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  34.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  35.     curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  36.     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  37.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  38.     $html= curl_exec($ch);
  39.     // La salvo de Entrada, para que no se me corra en la entrada a la base de datos
  40.     saveUrl($url, $prof,$patron, $html);
  41.  
  42.     // Mando error pero no corto porque si una url esta mal formada termina con la
  43.     // ejecucion
  44.     if (!$html) {
  45.         echo "<br />cURL error number:" .curl_errno($ch);
  46.         echo "<br />cURL error:" . curl_error($ch);
  47.     }  
  48.    
  49.     $dom = new DOMDocument();
  50.     $dom->loadHTML($html);
  51.  
  52.     $xpath = new DOMXPath($dom);
  53.     $hrefs = $xpath->evaluate("/html/body//a");
  54.  
  55.     for ($i = 0; $i < $hrefs->length; $i++) {
  56.         $href = $hrefs->item($i);
  57.         $url2 = $href->getAttribute('href');
  58.    
  59.         $var = strstr($url2, '#', true);
  60.         if ($var !== false ) {
  61.             $url2 = $var;
  62.         }
  63.         // Me aseguro que este bajo nuestro sitio.
  64.         if (strpos($url2, $patron) === false) {
  65.             continue;
  66.         }
  67.        
  68.         // Me aseguro que ya no este ingresada, para no iterar sobre ella misam
  69.         if ($url2 != $url && $url2 != '') {
  70.             // Se podria agregar un campo timestap para luego reescanera paginas
  71.             // que tuvieran una fecha menor.
  72.             // URL Unica para que falle y como es mysql poner INSERT INTO ...... ON DUPLICATE KEY ... en la funcion de guardado
  73.             $busqueda = mysql_query("SELECT weburl FROM spider WHERE weburl='$url2'");
  74.             $cantidad = mysql_num_rows($busqueda);
  75.             if ($prof <= 5 && $cantidad == 0) {
  76.                 extraer($url2, $prof++, $patron);  
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. function saveUrl($url, $prof, $patron, $html)
  84. {
  85.     $retorno = false;
  86.     $pos      = strpos($url, $patron);
  87.  
  88.     if( $prof >= 1) {
  89.         preg_match_all ("(<title>(.*)<\/title>)siU", $html, $title);
  90.         preg_match_all ("(<meta name=\"description\" content=\"(.*)\"\/>)siU", $html, $description);
  91.         preg_match_all ("(<meta name=\"keywords\" content=\"(.*)\"\/>)siU", $html, $keys);
  92.         $titulo = $title[1][0];
  93.         $descripcion = $description[1][0];
  94.         $keywords = $keys[1][0];
  95.         storeLink($titulo,$descripcion,$url,$keywords, $prof);
  96.         $retorno = true;
  97.         echo 'Guardada pagina : ' . $url . ' con profundidad ' . $prof . '<br>' . "\n\r";
  98.     }
  99.     return $retorno;
  100. }
  101.  
  102.  
  103. $url = "http://objetivophp.com";
  104. $patron = "http://objetivophp.com";
  105. $prof = 5;
  106.  
  107. extraer($url, 1, $patron);
  108. $errores = libxml_get_errors();
__________________
"La imaginación es más importante que el conocimiento. El conocimiento es limitado, mientras que la imaginación no" -- A.Einstein
objetivophp.com,twitter.com/objetivophp

Última edición por portalmana; 28/10/2012 a las 00:46