Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to remove html tags using HTML::Parser?

by rtlm (Novice)
on Jul 15, 2004 at 12:49 UTC ( [id://374630]=perlquestion: print w/replies, xml ) Need Help??

rtlm has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I have a whole html file read into a variable ($the_file), and i am trying to extract the tags from it with HTML::Parser.
I have gone over a few tutorials but they havn't been very helpful. Can anyone offer any suggestions or short pieces of code??
Thanks.

Yeah by extracting i mean removing and just leaving the plain text. will give it a shot. cheers.

The code works fine but instead of having the HTML stripped text printed to the screen, how can i have it stored in a scalar variable or an array??

Replies are listed 'Best First'.
Re: How to remove html tags using HTML::Parser?
by gellyfish (Monsignor) on Jul 15, 2004 at 13:07 UTC

    Really depends on what you mean by 'extract the tags from it' - if you mean just strip the tags and leave the text then:

    #!/usr/bin/perl + use strict; use warnings; + my $the_file =<<EOH; <html> <head><title>Test</title> </head> <body> <h1>Test Title</h1> <p>This is a test</p></body></html> EOH + use HTML::Parser; my $parser = HTML::Parser->new( text_h => [ sub { print shift; },"dtex +t" ]); + + $parser->parse($the_file);
    Will do what you want. Otherwise you had better explain in more detail.

    Update: Okay - but it's not much more difficult to get the stuff into a variable - this is an adaptation to put each separate bit of text into an array element:

    #!/usr/bin/perl use strict; use warnings; my $the_file =<<EOH; <html> <head><title>Test</title> </head> <body> <h1>Test Title</h1> <p>This is a test</p></body></html> EOH use HTML::Parser; my $parser = HTML::Parser->new( text_h => [ \&text_handler,"self,dtext +" ], start_document_h => [\&init, "self"] ) +; + $parser->parse($the_file); print @{$parser->{_private}->{text}}; sub init { my ( $self ) = @_; $self->{_private}->{text} = []; } sub text_handler { my ( $self, $text) = @_; push @{$self->{_private}->{text}}, $text; }
    Maybe I should update The very old tutorial.

    /J\

      Come on, don't be shy, point him to your old HTML::Parser tutorial. If you don't, I will. Oldie, but goodie.

      To the OP: reconsider HTML::TokeParser, or likely even better yet, HTML::TokeParser::Simple, as an alternative. It's a different approach, which I personally find a lot easier: you get a stream of tokens (an opening or closing tag, a piece of text), which you can read one at a time, like lines from a file, instead of a bunch of callbacks.

        I had forgotten quite how old that was - last updated on 12 December 1999, I can't remember how many times I have thought about updating it in the last 5 years, maybe if I get some interest I might get around to it ....

        /J\

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://374630]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found