Underc0de - La Casa de los Informáticos

Foros Generales => Dudas y pedidos generales => Mensaje iniciado por: ToracosLabs en Enero 05, 2016, 10:41:21 AM

Título: [SOLUCIONADO] Ayuda programando un spider.
Publicado por: ToracosLabs en Enero 05, 2016, 10:41:21 AM
 Saludos, estoy programando un script en php para poder extraer todos los libros de la web http://www.casadellibro.com/.

como los permalinks tienen los nombres de las entradas:

http://www.casadellibro.com/libro-el-ultimo-caton/9788408083450/1222964

se me ha ocurrido cambiar el ultimo número que es el ID del libro, ej:

http://www.casadellibro.com/libro-el-ultimo-caton/9788408083450/1

en el navegador funciona pero a la hora de hacer un file_get_contents() no funciona.

Por favor necesito ayuda. Gracias y disculpen las molestias!
Título: Re:Ayuda programando un spider.
Publicado por: blackdrake en Enero 05, 2016, 12:32:54 PM
Es bastante raro lo que te ocurre, probé también con un simple script en curl y tampoco funciona, pero por suerte, después de mucho intentarlo, pude hacerlo funcionar :D

Código (php) [Seleccionar]
<?php
//Script adaptado para recoger información de todos los libros de casadellibro.com.
//By blackdrake

//Id del libro
$id "2193770";

//Recogemos los datos de ese libro
$datos recoger_web('http://www.casadellibro.com/libro-el-ultimo-caton/9788408083450/'.$id);      

//Mostramos la pagina
echo $datos;


//Obtenemos los datos de la web

function recoger_web($url) {
    
$c curl_init();
    
curl_setopt($cCURLOPT_URL$url);
    
curl_setopt($cCURLOPT_RETURNTRANSFER1);
curl_setopt($cCURLOPT_SSL_VERIFYHOSTfalse);
    
curl_setopt($cCURLOPT_SSL_VERIFYPEERfalse);
    
curl_setopt($cCURLOPT_USERAGENT"Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
    
curl_setopt($cCURLOPT_MAXREDIRS10);
    
$follow_allowed = ( ini_get('open_basedir') || ini_get('safe_mode')) ? false true;
    
if ($follow_allowed) {
        
curl_setopt($cCURLOPT_FOLLOWLOCATION1);
    }
curl_setopt($cCURLOPT_CONNECTTIMEOUT9);
    
curl_setopt($cCURLOPT_REFERER$url);
    
curl_setopt($cCURLOPT_TIMEOUT60);
    
curl_setopt($cCURLOPT_AUTOREFERERtrue);
    
curl_setopt($cCURLOPT_ENCODING'gzip,deflate');

    
$data curl_exec($c);
    
$status curl_getinfo($c);
    
curl_close($c);
  

    if (
$status['http_code'] == 200) {
        return 
$data;
    } 

elseif ($status['http_code'] == 301 || $status['http_code'] == 302) {
        if (!
$follow_allowed) {
            if (empty(
$redirURL)) {
                if (!empty(
$status['redirect_url'])) {
                    
$redirURL $status['redirect_url'];
                }
            } if (empty(
$redirURL)) {
                
preg_match('/(Location:|URI:)(.*?)(\r|\n)/si'$data$m);
                if (!empty(
$m[2])) {
                    
$redirURL $m[2];
                }
            } if (empty(
$redirURL)) {
                
preg_match('/href\=\"(.*?)\"(.*?)here\<\/a\>/si'$data$m);
                if (!empty(
$m[1])) {
                    
$redirURL $m[1];
                }
            } if (!empty(
$redirURL)) {
                
$t debug_backtrace();
                return 
call_user_func($t[0]["function"], trim($redirURL), $post_paramtrs);
            }
        }
    }
return "Error en la url: $url!!<br/><br/> Petición enviada<b/>:<br/>" json_encode($status) . "<br/><br/>Web capturada (con el error):$data";
}


Saludos

Título: Re:Ayuda programando un spider.
Publicado por: ToracosLabs en Enero 05, 2016, 12:36:01 PM
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Es bastante raro lo que te ocurre, probé también con un simple script en curl y tampoco funciona, pero por suerte, después de mucho intentarlo, pude hacerlo funcionar :D

Código (php) [Seleccionar]
<?php
//Script adaptado para recoger información de todos los libros de casadellibro.com.
//By blackdrake

//Id del libro
$id "2193770";

//Recogemos los datos de ese libro
$datos recoger_web('http://www.casadellibro.com/libro-el-ultimo-caton/9788408083450/'.$id);      

//Mostramos la pagina
echo $datos;


//Obtenemos los datos de la web

function recoger_web($url) {
    
$c curl_init();
    
curl_setopt($cCURLOPT_URL$url);
    
curl_setopt($cCURLOPT_RETURNTRANSFER1);
curl_setopt($cCURLOPT_SSL_VERIFYHOSTfalse);
    
curl_setopt($cCURLOPT_SSL_VERIFYPEERfalse);
    
curl_setopt($cCURLOPT_USERAGENT"Mozilla/5.0 (Windows NT 6.1; rv:33.0) Gecko/20100101 Firefox/33.0");
    
curl_setopt($cCURLOPT_MAXREDIRS10);
    
$follow_allowed = ( ini_get('open_basedir') || ini_get('safe_mode')) ? false true;
    
if ($follow_allowed) {
        
curl_setopt($cCURLOPT_FOLLOWLOCATION1);
    }
curl_setopt($cCURLOPT_CONNECTTIMEOUT9);
    
curl_setopt($cCURLOPT_REFERER$url);
    
curl_setopt($cCURLOPT_TIMEOUT60);
    
curl_setopt($cCURLOPT_AUTOREFERERtrue);
    
curl_setopt($cCURLOPT_ENCODING'gzip,deflate');

    
$data curl_exec($c);
    
$status curl_getinfo($c);
    
curl_close($c);
    

    if (
$status['http_code'] == 200) {
        return 
$data;
    } 

elseif ($status['http_code'] == 301 || $status['http_code'] == 302) {
        if (!
$follow_allowed) {
            if (empty(
$redirURL)) {
                if (!empty(
$status['redirect_url'])) {
                    
$redirURL $status['redirect_url'];
                }
            } if (empty(
$redirURL)) {
                
preg_match('/(Location:|URI:)(.*?)(\r|\n)/si'$data$m);
                if (!empty(
$m[2])) {
                    
$redirURL $m[2];
                }
            } if (empty(
$redirURL)) {
                
preg_match('/href\=\"(.*?)\"(.*?)here\<\/a\>/si'$data$m);
                if (!empty(
$m[1])) {
                    
$redirURL $m[1];
                }
            } if (!empty(
$redirURL)) {
                
$t debug_backtrace();
                return 
call_user_func($t[0]["function"], trim($redirURL), $post_paramtrs);
            }
        }
    }
return "Error en la url: $url!!<br/><br/> Petición enviada<b/>:<br/>" json_encode($status) . "<br/><br/>Web capturada (con el error):$data";
}


Saludos

le voy a echar un vistazo. Muchas gracias máquina, te debo una!  ;D
Título: Re:Ayuda programando un spider.
Publicado por: blackdrake en Enero 05, 2016, 12:37:11 PM
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
le voy a echar un vistazo. Muchas gracias máquina, te debo una!  ;D

Avisa si te funciona :D

Saludos.
Título: Re:Ayuda programando un spider.
Publicado por: ToracosLabs en Enero 05, 2016, 01:05:28 PM
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
le voy a echar un vistazo. Muchas gracias máquina, te debo una!  ;D

Avisa si te funciona :D

Saludos.

Perfectamente, ahora un for y para la base de datos!  8)
Título: Re:Ayuda programando un spider.
Publicado por: blackdrake en Enero 05, 2016, 01:10:31 PM
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
le voy a echar un vistazo. Muchas gracias máquina, te debo una!  ;D

Avisa si te funciona :D

Saludos.

Perfectamente, ahora un for y para la base de datos!  8)

Perfecto, doy el tema como solucionado, cualquier cosa, vuelves a postear :D

Saludos.