Ver Mensaje Individual
  #4 (permalink)  
Antiguo 27/10/2012, 21:16
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)
  16. {
  17.     $query = "INSERT INTO spider (webTitulo, webDescripcion, weburl, webkeywords) VALUES ('$titulo', '$descripcion', '$url', '$keywords')";
  18.     mysql_query($query) or die('Error, falló la inserción de datos');
  19. }
  20.  
  21. function extraer($url, $prof, $patron)
  22. {
  23.  
  24.     $userAgent = 'Interredu';
  25.  
  26.     $ch = curl_init();
  27.     curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  28.     curl_setopt($ch, CURLOPT_URL,$url);
  29.     curl_setopt($ch, CURLOPT_HTTPHEADER, array(("Accept-Language: es-es,en")));
  30.     curl_setopt($ch, CURLOPT_FAILONERROR, true);
  31.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  32.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  33.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  34.     curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  35.     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  36.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  37.     $html= curl_exec($ch);
  38.  
  39.     if (!$html) {
  40.         echo "<br />cURL error number:" .curl_errno($ch);
  41.         echo "<br />cURL error:" . curl_error($ch);
  42.         exit;
  43.     }
  44.  
  45.     $dom = new DOMDocument();
  46.     $dom->loadHTML($html);
  47.  
  48.     $xpath = new DOMXPath($dom);
  49.     $hrefs = $xpath->evaluate("/html/body//a");
  50.  
  51.     for ($i = 0; $i < $hrefs->length; $i++) {
  52.         $href = $hrefs->item($i);
  53.         $url2 = $href->getAttribute('href');
  54.    
  55.         $var = strstr($url2, '#', true);
  56.         if ($var !== false ) {
  57.             $url2 = $var;
  58.         }
  59.    
  60.         if ($url2 != $url && $url2 != '') {
  61.             if (saveUrl($url2, $prof,$patron, $html)) {
  62.                 extraer($url2, $prof ++, $patron);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. function saveUrl($url, $prof, $patron, $html)
  70. {
  71.     $retorno = false;
  72.     $busqueda = mysql_query("SELECT weburl FROM spider WHERE weburl='$url'");
  73.     $cantidad = mysql_num_rows($busqueda);
  74.     $pos      = strpos($url, $patron);
  75.  
  76.     if( $prof <= 1 and $cantidad == 0 and $pos !== false) {
  77.         preg_match_all ("(<title>(.*)<\/title>)siU", $html, $title);
  78.         preg_match_all ("(<meta name=\"description\" content=\"(.*)\"\/>)siU", $html, $description);
  79.         preg_match_all ("(<meta name=\"keywords\" content=\"(.*)\"\/>)siU", $html, $keys);
  80.         $titulo = $title[1][0];
  81.         $descripcion = $description[1][0];
  82.         $keywords = $keys[1][0];
  83.         storeLink($titulo,$descripcion,$url,$keywords);
  84.         $retorno = true;
  85.         echo 'Guardada pagina : ' . $url . ' con profundidad ' . $prof . '<br>' . "\n\r";
  86.     }
  87.     return $retorno;
  88. }
  89.  
  90.  
  91. $url = "http://www.forosdelweb.com/f18";
  92. $patron = "http://www.forosdelweb.com/f18";
  93. $prof = 1;
  94.  
  95. extraer($url, 1, $patron);
  96. $errores = libxml_get_errors();

Tienes que refinar metas y keywoards,
Cuando lo tengas mas refinado me lo pasas.

Saludos
__________________
"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