IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

Problème récupération lien (attribut) dans une balise (html)


Sujet :

Langage Perl

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Octobre 2007
    Messages
    99
    Détails du profil
    Informations forums :
    Inscription : Octobre 2007
    Messages : 99
    Par défaut Problème récupération lien (attribut) dans une balise (html)
    Bonjour à tous,

    Mon problème est de vouloir récupérer un lien qui se situe dans cette balise :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href="http://www.pharmgkb.org/do/serve?objId=PA227" class=l onmousedown="return clk(this.href,'','','res','1','')">
    mon adresse à récuperer est http://www.pharmgkb.org/do/serve?objId=PA227.

    Début de code uniquement dans le parser :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #Parser
    my $parser = HTML::Parser->new(start_h => [\&start_rtn,"tag, attr"],
                    text_h => [\&text_rtn, "text, attr"],
                    end_h => [\&end_rtn, "tag"]
                    );
     
    sub start_rtn {
    	my ($tag, $attr) = @_;
     
    #$Flag = 1 (Récupération URL sur pharmGKB.org)
    	if ($tag =~ /^a$/ 		
    		and defined $attr->{href}  
        		and $attr->{href} =~ /http:\/\/www.pharmgkb.org\/do\/serve?objId=.[0-9]$/
       		and defined $attr->{class} 
        		and $attr->{class} =~ /^l$/
    		and defined $attr->{onmousedown}
    		and $attr->{onmousedown} =~ /return clk(this.href,'','','res','1','')/ ){    
      		$flag = 3;
    		my $URL = $attr->{href};
    		print "$URL";
    	}
    }
    Et on ne m'affiche rien...Ainsi je vous demande vos aides et commentaires...Merci

  2. #2
    Expert confirmé
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Par défaut
    Les parenthèses () et ? sont des caractères spéciaux dans les regexps, il faut les échapper pour qu'elles soient traités normalement.
    (Par ailleurs, ça m'a l'air un peu trop précis comme réquisition, est-ce que tu as vraiment besoin de matcher "onmousedown" pour sélectionner la bonne balise ?)

    Et aussi, lorsque tu as 3 fois les mêmes instructions, crée une fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    sub check { defined $_[0] and $_[0] =~ m/$_[1]/ }
     
    # ...
     
      if ($tag =~ /^a$/ 		
          and check( $attr->{href}, qr{^http://www.pharmgkb.org/do/serve\?objId=} )
          and check( $attr->{class}, qr{^l$} )
          and check( $attr->{onmousedown}, qr{return clk\(this\.href,'','','res','1',''\)} ) ){    
        $flag = 3;
        my $URL = $attr->{href};
        print "$URL";
      }
    --
    Jedaï

  3. #3
    Membre confirmé
    Inscrit en
    Octobre 2007
    Messages
    99
    Détails du profil
    Informations forums :
    Inscription : Octobre 2007
    Messages : 99
    Par défaut
    Merci pour ce changement !!! Cela risque de fonctionner je vous remercie...

    Cependant je ne comprend pas, ma console, après exécution du programme, m'indique :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    http://www.google.com/search?hl=fr&q=PharmGKB+LDLR&btnG=Rechercher&meta=
    Use of uninitialized value in subroutine entry at Essai_1.pl line 66.
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    #!/usr/bin/perl
    use strict; 
    use warnings;
     
    use HTML::Parser;
    use LWP::Simple;
     
    #Variables
    my $baseurl = 'http://www.google.com/';
    my $flag = 0;
     
     
    die "usage: $0 site gene\n"
        if @ARGV != 2;
     
     
    my $pharmGKB = $ARGV[0];
    my $nom_gene = $ARGV[1];
     
    #Page URL où parser
    my $url = $baseurl.'search?hl=fr&q='.$pharmGKB.'+'.$nom_gene.'&btnG=Rechercher&meta=';
    print "$url\n";
    my $PAGE = get($url); 
     
     
    sub check { defined $_[0] and $_[0] =~ m/$_[1]/ }
     
    #Parser
    my $parser = HTML::Parser->new(start_h => [\&start_rtn,"tag, attr"],
                    text_h => [\&text_rtn, "text"],
                    end_h => [\&end_rtn, "tag"]
                    );
     
    sub start_rtn {
     
    	my ($tag, $attr) = @_;
    	if ($tag =~ /^a$/ 		
          		and check( $attr->{href}, qr{^http://www.pharmgkb.org/do/serve\?objId=} )
         	 	and check( $attr->{class}, qr{^l$} )
          		and check( $attr->{onmousedown}, qr{return clk\(this\.href,'','','res','1',''\)} ) ){    
        		$flag = 1;
        		my $URL = $attr->{href};
        		print "$URL";
      	}
    }
     
    sub text_rtn {
    	my ($text) = @_;
      	$text =~ s/\n/ /g;
     
    	if($flag == 1){                
           		print "$text\n";
        	}
    }
     
    sub end_rtn {
    	my ($tag) = @_;
     
    	if ($tag =~ /^\/a$/ && $flag == 1){
                 	$flag = 0;
    	}
     
    }
     
    #start parsing
    $parser->parse($PAGE);
     
    #end parser
    $parser->eof;
    Pourquoi il m'affiche cela (en sachant que la ligne 66 correspond à $parser->parse($PAGE). De plus mes autres programmes étaient basés sur le même "parser" et fonctionnaient très bien...

    Merci d'avance

  4. #4
    Expert confirmé
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Par défaut
    Cela risque de fonctionner je vous remercie...
    Intéressante formulation...

    Pourquoi il m'affiche cela (en sachant que la ligne 66 correspond à $parser->parse($PAGE). De plus mes autres programmes étaient basés sur le même "parser" et fonctionnaient très bien...
    Parce que Google a décidé de refuser l'accès de ses pages de recherche au robots, et rejette donc notre UserAgent qui s'identifie comme "libwww/5.8", il faut changer cette identification pour accéder à la page, le code suivant te montre comment : (et simplifie un peu tes réquisitions draconiennes et exagérées)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    #!/usr/bin/perl
    use strict; 
    use warnings;
     
    use HTML::Parser;
    use LWP::Simple qw($ua get);
     
    #Variables
    my $baseurl = 'http://www.google.com/';
    my $flag = 0;
     
     
    die "usage: $0 site gene\n"
        if @ARGV != 2;
     
     
    my $pharmGKB = $ARGV[0];
    my $nom_gene = $ARGV[1];
     
    #Page URL où parser
    my $url = $baseurl.'search?hl=fr&q='.$pharmGKB.'+'.$nom_gene.'&btnG=Rechercher&meta=';
    print "$url\n";
     
    $ua->agent('Mozilla/5.0');
    my $page = get($url);
    die "Can't get $url\n" unless defined $page;
     
    sub check { defined $_[0] and $_[0] =~ m/$_[1]/ }
     
    #Parser
    my $parser = HTML::Parser->new(start_h => [\&start_rtn,"tag, attr"],
    			       text_h => [\&text_rtn, "text"],
    			       end_h => [\&end_rtn, "tag"]
    );
     
    sub start_rtn {
      my ($tag, $attr) = @_;
      if ($tag =~ /^a$/ 		
          and check( $attr->{href}, qr{^http://www.pharmgkb.org/do/serve\?objId=[^&]+$} ) ){
        $flag = 1;
        my $URL = $attr->{href};
        print "$URL\n";
      }
    }
     
    sub text_rtn {
    	my ($text) = @_;
      	$text =~ s/\n/ /g;
     
    	if($flag == 1){                
           		print "$text\n";
        	}
    }
     
    sub end_rtn {
    	my ($tag) = @_;
     
    	if ($tag =~ /^\/a$/ && $flag == 1){
                 	$flag = 0;
    	}
     
    }
     
    #start parsing
    $parser->parse($page);
     
    #end parser
    $parser->eof;
    --
    Jedaï

  5. #5
    Membre confirmé
    Inscrit en
    Octobre 2007
    Messages
    99
    Détails du profil
    Informations forums :
    Inscription : Octobre 2007
    Messages : 99
    Par défaut
    Intéressante formulation...
    Je te remercie Jedai

    En tout cas, je viens d'essayer avec ton code...TOUT FONCTIONNE !!!
    Grand Merci de ta part

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [RegEx] fonction d'ajout d'attribut dans une balise html
    Par the magic developer dans le forum Langage
    Réponses: 4
    Dernier message: 30/10/2008, 12h48
  2. [HTML::Parser] Problème Attributs dans une balise
    Par stansoad0108 dans le forum Modules
    Réponses: 4
    Dernier message: 26/03/2008, 11h49
  3. problème xsl : inclure une donnée xml dans une balise html
    Par djodjo dans le forum XSL/XSLT/XPATH
    Réponses: 3
    Dernier message: 03/01/2003, 09h24
  4. [XSLT] inclure du XSL dans une balise html
    Par iaa dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 05/08/2002, 15h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo