
Yuvraj
Deleted
Nov 15, 2000, 1:57 AM
Post #1 of 1
(181 views)
|
Hi, Iam currently working in a project which needs HTML::parser package.My aim is to extract data(tags&text)from a HTML page. I have a code for extracting the href attribute from an anchor tag .But it is not working. Could you please help me to debug any error in this code. The error i get is "use of uninitialised value" The code is #!/usr/bin/perl -w # This program will print out all <a href=".."> links in a # document together with the text that goes with it. use HTML::Parser 2.2.3; my $p = HTML::Parser->new(api_version => 3, start_h => [\&a_start_handler, "self,tagname,attr"]); $p->parse_file(shift | | die) | | die $!; sub a_start_handler { my($self, $tag, $attr) = @_; return unless $tag eq "a"; return unless exists $attr->{href}; print "A $attr->{href}\n"; $self->handler(text => [], "dtext" ); $self->handler(start => \&img_handler); $self->handler(end => \&a_end_handler, "self,tagname"); } sub img_handler { my($self, $tag, $attr) = @_; return unless $tag eq "img"; push(@{$self->handler("text")}, [$attr->{alt} | | ""]); } sub a_end_handler { my($self, $tag) = @_; my $text = join("", map $_->[0], @{$self->handler("text")}); $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g; print "T $text\n"; $self->handler("text", undef); $self->handler("start", \&a_start_handler); $self->handler("end", undef); } Iam actually struggling to get this code running.I think that you can help me Can you please reply as soon as possible.It will be a great help for me.reply ASAP
|