in reply to Re: Ignoring specific html tags before parsing
in thread Ignoring specific html tags before parsing
Meanwhile, I found an alternative, but seems it is quite slower than what we could have achieved with HTML::Parser.my @array; my $p = HTML::Parser->new(api_version => 3, handlers => { text => [\@array, "text"]}); $p->ignore_tags(qw(table img)); $p->parse($page); print "Size of array=$#array\n"; foreach my $aline (@array) { print $aline; } print "\n";
This above use of TokeParser gives lot of broken text. Which could be better way? Thanksmy $link = 'somelinek'; my $page = get($link) or die $!; my $stream = HTML::TokeParser->new(\$page); my $doparse = 1; ## 0 means don't parse while (my $token = $stream->get_token) { if ($token->[0] eq 'S') { if ($token->[1] eq 'table') { $doparse = 0; } elsif ($token->[1] eq 'img') { ;; } } elsif ($token->[0] eq 'E' and $token->[1] eq 'table') { $doparse = 1; } elsif ($token->[0] eq 'C') { ;; } elsif ($token->[0] eq 'T' and $doparse eq 1) { # text process the text in $token->[1] # skip: empty lines, " " if (defined ($token->[1])) { $token->[1] =~ s/ / /ig; $token->[1] =~ s/’/'/ig; $token->[1] =~ s/[7-8];/"/ig; $token->[1] =~ s/—//ig; $token->[1] =~ s/&/&/ig; $token->[1] =~ s/-{2,}//ig; print "$token->[1]"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Ignoring specific html tags before parsing
by Anonymous Monk on Oct 07, 2013 at 07:22 UTC | |
by Anonymous Monk on Oct 08, 2013 at 01:36 UTC | |
by Anonymous Monk on Oct 08, 2013 at 02:21 UTC | |
|
Re^3: Ignoring specific html tags before parsing
by Anonymous Monk on Oct 07, 2013 at 07:30 UTC | |
by Anonymous Monk on Oct 07, 2013 at 07:31 UTC |