Página 1 de 1

Expresión regular en Google

NotaPublicado: 2012-07-07 16:53 @745
por BigBear
Tengo el siguiente código.

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use LWP::UserAgent;
  2.  
  3. my $nave = LWP::UserAgent->new;
  4. $nave->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.12) Gecko/20080201Firefox/2.0.0.12");
  5. $nave->timeout(5);
  6.  
  7. $code = toma("http://www.google.com.ar/search?q=test&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:es-ES:official&client=firefox-a");
  8. while($code=~/<a href="(.*?)" class=l onmousedown/mig) {
  9. print $1."\n";
  10. }
  11.  
  12. sub toma {
  13. return $nave->get($_[0])->content;
  14. }
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4


El problema es que la expresión regular no funciona. El patrón es así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
<a href="http://www.psicoactiva.com/tests.htm" class=l onmousedown
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


¿ Alguien me podría ayudar ?

Re: Expresión regular en Google

NotaPublicado: 2012-07-07 17:56 @788
por explorer
Pues a mí me funciona...

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use strict;             # programación estricta de Perl
  3. use warnings;           # activar las advertencias
  4. use diagnostics;        # advertencias realmente ruidosas
  5.  
  6. my $code = '
  7. <a href="http://www.psicoactiva.com/tests.htm" class=l onmousedown
  8. <a href="http://www.psicoactiva.com/tests.htm" class=l onmousedown
  9. <a href="http://www.psicoactiva.com/tests.htm" class=l onmousedown
  10. ';
  11.  
  12. while ($code =~ /<a href="(.*?)" class=l onmousedown/mig) {
  13.     print "$1\n";
  14. }
  15.  
  16. __END__
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Re: Expresión regular en Google

NotaPublicado: 2012-07-07 18:01 @792
por BigBear
Sí, ya me fijé que el problema viene del código cuando cargo con Google. Al parecer devuelve dos contenidos diferentes; uno cuando lo cargo con Firefox y otro diferente cuando lo cargo con el script en Perl.
Recuerdo que encontré esta expresión regular en Python: "(?<=\"r\"><. href=\")[^\"]+" que hace que funcione desde cualquier lado.

El problema es que no tengo ni idea de cómo traducirla a Perl.

¿ Vos sabes cómo la puedo traducir ?

Re: Expresión regular en Google

NotaPublicado: 2012-07-07 22:13 @967
por explorer
No sé nada de Python, pero si sigue las mismas reglas de expresiones regulares que en Perl,

(?<=\"r\"><. href=\")[^\"]+

significaría:
  • buscar por "r"><. href=" pero no lo incluye en el resultado de la búsqueda, seguido de
  • [^\"]+, que es coincidir con cualquier cosa hasta antes de la siguiente comilla doble.
Lo que ya no sé es cómo indica la captura de lo que quiere obtener.

En Perl se escribiría así:

/(?<="r"><. href=")(.+?)"/

pero que puede quedar reducido a

/"r"><. href="(.+?)"/

que... me gusta más :) Al menos, lo veo más claro.

Re: Expresión regular en Google

NotaPublicado: 2012-07-08 11:46 @532
por BigBear
¡Ja!, me quedó perfecto, pero tengo otra duda molesta.

El código quedó así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use LWP::UserAgent;
  2. use HTML::LinkExtor;
  3.  
  4. my $nave = LWP::UserAgent->new;
  5. $nave->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.12) Gecko/20080201Firefox/2.0.0.12");
  6. $nave->timeout(5);
  7. $code = toma("http://www.google.com.ar/search?q=test&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:es-ES:official&client=firefox-a");
  8. while($code=~/(?<="r"><. href=")(.+?)"/mig) {
  9. my $url = $1;
  10. print $url."\n";
  11. }
  12.  
  13. sub toma {
  14. return $nave->get($_[0])->content;
  15. }
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


El código me devuelve esto:

Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
/url?q=http://www.psicoactiva.com/tests.htm&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImD
Bw&amp;ved=0CBMQFjAA&amp;usg=AFQjCNGZGIsAW0mPEq6BeSCLimKfHb33zg
/url?q=http://analizame.com/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImDBw&amp;ved=0CBs
QFjAB&amp;usg=AFQjCNG9ZAKsuRNEXOpwGu6F2ac5ze0PKg
/url?q=http://amor.rincondelvago.com/tests/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImD
Bw&amp;ved=0CCIQFjAC&amp;usg=AFQjCNHv3wLUcdIOJioeSgfZ8N24mDw1lA
/url?q=http://www.mentat.com.ar/testvarios.htm&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTur
ImDBw&amp;ved=0CCkQFjAD&amp;usg=AFQjCNGmRxeHVdftB8yj-AsbX_KDalhSbQ
/url?q=http://www.helios3000.net/tests/eq.shtml&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTu
rImDBw&amp;ved=0CCwQFjAE&amp;usg=AFQjCNFPF9b_vqbNZjPBONtrEefZtqhbXg
/url?q=http://www.speedtest.net/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImDBw&amp;ved=
0CDMQFjAF&amp;usg=AFQjCNHLE9yuqV3Qc54dNhp792tKkIVJHg
/url?q=http://www.testdevelocidad.es/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImDBw&amp
;ved=0CDYQFjAG&amp;usg=AFQjCNGT7OT7Kr_UUTxn0NTdncBGTYmAQA
/url?q=http://acid3.acidtests.org/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurImDBw&amp;ve
d=0CDkQFjAH&amp;usg=AFQjCNFO2lJpQQP9JdpWBI-qhykvoMNNAg
/url?q=http://www.ticketek.com.ar/ajax/lightboxTicket.php&amp;sa=U&amp;ei=qbj5T9
rSD5SQ8wTurImDBw&amp;ved=0CDwQFjAI&amp;usg=AFQjCNFbzTog9Ha-pNk1fUHXWw3zuc7mFw
/interstitial?url=http://www.tododetest.com/&amp;sa=U&amp;ei=qbj5T9rSD5SQ8wTurIm
DBw&amp;ved=0CD4QFjAJ
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4


¿ Me podrías decir cómo perfeccionar la expresión regular para que saque la URL de cada uno ?

Re: Expresión regular en Google

NotaPublicado: 2012-07-08 18:18 @804
por explorer
Pero si eso ya son URL... ;)

Otra cosa es que quieras extraer la URL que está asociada al parámetro 'q', algo que se puede hacer fácil con una expresión regular sencilla, ya que sabemos dónde empieza y dónde acaba.

Re: Expresión regular en Google

NotaPublicado: 2012-07-08 18:20 @805
por BigBear
sip, pregunta al vicio el código quedó así:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use LWP::UserAgent;
  2.  
  3. my $nave = LWP::UserAgent->new;
  4. $nave->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.12) Gecko/20080201Firefox/2.0.0.12");
  5. $nave->timeout(5);
  6.  
  7. my @test = google( "noticias.php+id", "10" );
  8.  
  9. #for(@test) { print $_."\n"; }
  10.  
  11. sub google {
  12.     my ( $a, $b ) = @_;
  13.     my @founds;
  14.     for ( $pages = 10; $pages <= $b; $pages = $pages + 10 ) {
  15.         $code = toma( "http://www.google.com.ar/search?hl=&q=" . $a . "&start=$pages" );
  16.         while ( $code =~ /(?<="r"><. href=")(.+?)"/mig ) {
  17.             my $url = $1;
  18.             if ( $url =~ /\/url\?q\=(.*?)\&amp\;/ ) {
  19.                 print $1. "\n";
  20.                 push( @founds, $1 );
  21.             }
  22.         }
  23.     }
  24.     my @founds = repes( cortar(@founds) );
  25.     return @founds;
  26. }
  27.  
  28. sub repes {
  29.     my @limpio;
  30.     foreach $test (@_) {
  31.         push @limpio, $test unless $repe{$test}++;
  32.     }
  33.     return @limpio;
  34. }
  35.  
  36. sub cortar {
  37.     my @nuevo;
  38.     for (@_) {
  39.         if ( $_ =~ /=/ ) {
  40.             @tengo = split( "=", $_ );
  41.             push( @nuevo, @tengo[0] . "=" );
  42.         }
  43.         else {
  44.             push( @nuevo, $_ );
  45.         }
  46.     }
  47.     return @nuevo;
  48. }
  49.  
  50. sub toma {
  51.     return $nave->get( $_[0] )->content;
  52. }
  53.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4