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

Modules Perl Discussion :

Problème initialisation tableau code HTML::Parser


Sujet :

Modules Perl

  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 initialisation tableau code HTML::Parser
    Bonjour à tous,

    J'ai écrit un programme pour récupérer des informations dans une page HTML, grâce au module HTML:: Parser
    Cela fonctionne (j'arrive à les récupérer) !!! Cependant dans ma console on inscrit plusieurs fois :

    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 52
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 63
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 34
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 40
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 46
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 69
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 34
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 40
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 34
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 40
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 34
    Use of uninitialized value in pattern match (m//) at "nom_fichier.pl" line 40
    ...........

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    #!/usr/bin/perl
     
    use warnings;
    use HTML::Form;
    use LWP::Simple;
    use HTML::Parser();
    use HTML::LinkExtor;
     
    #Récupération arguments
    my $filename = 'myfile.html';
     
    #my $filename = get("http://www.pharmgkb.org/do/serve?objId=PA227&objCls=Gene" || shift);
     
    my $flag = 0;
     
    #open file
    open (my $fh, $filename) or die "$!\n";
     
    #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 { 
    # Execute when start tag is encountered
     
       	my ($tag, $attr) = @_;
     
    	if ($tag =~ /^title$/){										
    		print "On a trouvé la balise : $tag \n";						
                 	$flag = 1;										
    	}
     
    	if ($tag =~ /^td$/ && $attr->{class} =~ /^fieldName$/){			#Il y en a 2 			
    		print "On a trouvé la balise : $tag \n";						
    		print "L'attribut dans cette balise est : $attr->{class}\n";								
    		$flag = 2;										
    	}	
     
    	if ($tag =~ /^td$/ && $attr->{class} =~ /^fieldValue$/){			#Il y en a 2			
    		print "On a trouvé la balise : $tag \n";
    		print "L'attribut dans cette balise est : $attr->{class}\n";
    		$flag = 3;
    	}
     
    	if ($tag =~ /^h3$/ && $attr->{class} =~ /^noTopPad$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{class}\n";
    		$flag = 4;
    	}	
     
    	if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA448500&objCls=Drug$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 5;
    	}
     
    	if ($tag =~ /^h3$/){	
    		print "on a trouvé la balise : $tag \n";
    		$flag = 6;
    	}
     
    	if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA444517&objCls=Disease$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 7;
    	}
     
    	if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA447159&objCls=Disease$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 8;
    	}				
    }
     
     sub text_rtn {
    	my ($text) = @_;
     
    	if ($flag == 1){				
    		print "on a flag : $flag \n";
    		print "Le titre de cette page est : $text \n";
    	}
     
    	if($flag == 2){
    		print "on a flag : $flag \n";
    		print "On cherche => $text \n";
    	} 
     
    	if($flag == 3){
    		print "on a flag :  $flag \n";
    		print "Les alternatives recherchées sont : $text \n";
    	}
     
    	if($flag == 4){
    		print "on a flag :  $flag \n";
    		print "$text \n";
    	}
    	if($flag == 5){
    		print "on a flag :  $flag \n";
    		print "Un médicament relatif à ce gène est : $text \n";
    	}	
    	if($flag == 6){
    		print "on a flag :  $flag \n";
    		print "$text \n";
    	}
    	if($flag == 7){
    		print "on a flag :  $flag \n";
    		print "Une maladie relative à ce gène est : $text \n";
    	}
    	if($flag == 8){
    		print "on a flag :  $flag \n";
    		print "Une maladie relative à ce gène est : $text \n";
    	}						
    }
     
     sub end_rtn {
         my ($tag) = @_;
    	if ($tag =~ /^\/title$/){
    		print "On a fini par la balise : $tag \n";
                    $flag = 0;
    	}
    	if ($tag =~ /^\/td$/ && ($flag==2 || $flag==3)){
    		print "on a fini par la balise : $tag \n";
    		$flag = 0;
    	}
    	if ($tag =~ /^\/h3$/ && ($flag==4 || $flag==6)){
    		print "on a fini : $tag\n";
    		$flag = 0;
    	}
    	if ($tag =~ /^\/a$/ && ($flag==5 || $flag==7 || $flag==8)){
    		print "on a fini : $tag\n";
    		$flag = 0;
    	}
     
     
    }
     
     
    #start parsing
    $parser->parse_file($fh);
     
     
    #end parser
    $parser ->eof;
    La ligne 34 => if ($tag =~ /^td$/ && $attr->{class} =~ /^fieldName$/){
    ligne 40 => if ($tag =~ /^td$/ && $attr->{class} =~ /^fieldValue$/){
    ligne 46 =>if ($tag =~ /^h3$/ && $attr->{class} =~ /^noTopPad$/){
    ligne 52 => if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA448500&objCls=Drug$/){
    ligne 63 => if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA444517&objCls=Disease$/){
    ligne 69 => if ($tag =~ /^a$/ && $attr->{href} =~ /^\/do\/serve\?objId=PA447159&objCls=Disease$/){

    Ce serait peut-être un problème d'initialisation dans mon tableau : my ($tag, $attr) = @_;

    Je ne sais pas comment y remédier...

    Merci pour vos commentaires

  2. #2
    Membre chevronné Avatar de iblis
    Inscrit en
    Janvier 2007
    Messages
    510
    Détails du profil
    Informations personnelles :
    Âge : 58

    Informations forums :
    Inscription : Janvier 2007
    Messages : 510
    Par défaut
    Le problème se pose parce que dans certains cas le hash n'a pas de valeur pour la clé attendue. Ajoute un test pour vérifier :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     if ($tag =~ /^td$/ and defined $attr->{class} and $attr->{class} =~ /^fieldName$/){

  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 !!!

    Il n'y a plus d'erreurs dans la console !!! Je retrouve bien toutes mes informations !!!

    Je vous remercie

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    #!/usr/bin/perl
     
    use warnings;
    use HTML::Form;
    use LWP::Simple;
    use HTML::Parser();
    use HTML::LinkExtor;
     
    #Récupération arguments
    my $filename = 'myfile.html';
     
    #my $filename = get("http://www.pharmgkb.org/do/serve?objId=PA227&objCls=Gene" || shift);
     
    my $flag = 0;
     
    #open file
    open (my $fh, $filename) or die "$!\n";
     
    #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 { 
    # Execute when start tag is encountered
     
       	my ($tag, $attr) = @_;
     
    	if ($tag =~ /^title$/){										
    		print "On a trouvé la balise : $tag \n";						
                 	$flag = 1;										
    	}
     
    	if ($tag =~ /^td$/ and defined $attr->{class} and $attr->{class} =~ /^fieldName$/){ 			
    		print "On a trouvé la balise : $tag \n";						
    		print  "L'attribut dans cette balise est : $attr->{class}\n";								
    		$flag = 2;										
    	}	
     
    	if ($tag =~ /^td$/ and defined $attr->{class}  and $attr->{class} =~ /^fieldValue$/){					
    		print "On a trouvé la balise : $tag \n";
    		print "L'attribut dans cette balise est : $attr->{class}\n";
    		$flag = 3;
    	}
     
    	if ($tag =~ /^h3$/ and defined $attr->{class}  and $attr->{class} =~ /^noTopPad$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{class}\n";
    		$flag = 4;
    	}	
     
    	if ($tag =~ /^a$/ and defined $attr->{href}  and $attr->{href} =~ /^\/do\/serve\?objId=PA448500&objCls=Drug$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 5;
    	}
     
    	if ($tag =~ /^h3$/){	
    		print "on a trouvé la balise : $tag \n";
    		$flag = 6;
    	}
     
    	if ($tag =~ /^a$/ and defined $attr->{href}  and $attr->{href} =~ /^\/do\/serve\?objId=PA444517&objCls=Disease$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 7;
    	}
     
    	if ($tag =~ /^a$/ and defined $attr->{href} and $attr->{href} =~ /^\/do\/serve\?objId=PA447159&objCls=Disease$/){	
    		print "on a trouvé la balise : $tag \n";
    		print "$attr->{href}\n";
    		$flag = 8;
    	}				
    }
     
     sub text_rtn {
    	my ($text) = @_;
     
    	if ($flag == 1){				
    		print "on a flag : $flag \n";
    		print "Le titre de cette page est : $text \n";
    	}
     
    	if($flag == 2){
    		print "on a flag : $flag \n";
    		print "On cherche => $text \n";
    	} 
     
    	if($flag == 3){
    		print "on a flag :  $flag \n";
    		print "Les alternatives recherchées sont : $text \n";
    	}
     
    	if($flag == 4){
    		print "on a flag :  $flag \n";
    		print "$text \n";
    	}
    	if($flag == 5){
    		print "on a flag :  $flag \n";
    		print "Un médicament relatif à ce gène est : $text \n";
    	}	
    	if($flag == 6){
    		print "on a flag :  $flag \n";
    		print "$text \n";
    	}
    	if($flag == 7){
    		print "on a flag :  $flag \n";
    		print "Une maladie relative à ce gène est : $text \n";
    	}
    	if($flag == 8){
    		print  "on a flag :  $flag \n";
    		print "Une maladie relative à ce gène est : $text \n";
    	}						
    }
     
     sub end_rtn {
         my ($tag) = @_;
    	if ($tag =~ /^\/title$/){
    		print "On a fini par la balise : $tag \n";
                    $flag = 0;
    	}
    	if ($tag =~ /^\/td$/ && ($flag==2 || $flag==3)){
    		print "on a fini par la balise : $tag \n";
    		$flag = 0;
    	}
    	if ($tag =~ /^\/h3$/ && ($flag==4 || $flag==6)){
    		print "on a fini : $tag\n";
    		$flag = 0;
    	}
    	if ($tag =~ /^\/a$/ && ($flag==5 || $flag==7 || $flag==8)){
    		print "on a fini : $tag\n";
    		$flag = 0;
    	}
     
     
    }
     
     
    #start parsing
    $parser->parse_file($fh);
     
     
    #end parser
    $parser ->eof;
    Maintenant pour améliorer mon code, je voudrais non plus prendre comme fichier d'entrée : "myfile.html", mais prendre directement l'adresse URL (Je l'ai mise en commentaire). Cependant je ne sais pas si je dois installer un autre module, ou....

    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
    Tu as déjà importé LWP::Simple, il te suffit d'utiliser get() pour récupérer le contenu de la page et ensuite parser directement celui-ci.

    --
    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
    Merci pour ton commentaire.

    Mais j'ai vu où était mon erreur :

    my $filename =get('http://www.pharmgkb.org/do/serve?objId=PA227&objCls=Gene')

    et pour débuter mon parsage, au lieu d'écrire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     #start parsing  
    $parser->parse_file($fh);
    j'ai écris :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $parser->parse($filename);
    Et cela fonctionne !!!

    Encore Merci

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

Discussions similaires

  1. Problème initialisation tableau 2D
    Par MasterNico dans le forum Débuter
    Réponses: 10
    Dernier message: 21/10/2013, 13h11
  2. [RegEx] problème expression dans code html
    Par flagodzki dans le forum Langage
    Réponses: 2
    Dernier message: 31/08/2012, 14h46
  3. Problème d'intallation de Html-Parser
    Par nkta1 dans le forum Solaris
    Réponses: 5
    Dernier message: 22/01/2010, 14h37
  4. Réponses: 17
    Dernier message: 18/07/2008, 14h29
  5. Problème de montage d'un tableau en HTML...
    Par David.V dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 04/02/2005, 08h38

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