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

My question is with HTML::Parser but I also wonder if I am approaching this issue the correct way. I am trying to parse through a web-page and pull out the information I want which I would like to write into a text file or into memory. Here is a sample of some of the web page:
tr </tr><tr> <td><font size="2" face="Arial,Helvetica"> <hr size="2" width="100%"> Title: <b>2004-10-15 Off Season Rosters</b><BR> Post by <b>sydvicious</b> on <b>Oct 15<sup>th</sup>, 2004, 7:24pm< +/b> <hr width="100%" size="1"> I know, the keeper years all say &quot;1&quot;.<br><br><pre><br>TR +ANSACTIONS:<br><br>Scott - Activated Catalanotto_Frank/TOR; Waived Ca +stilla_Vinny/COL<br>Scott - Activated Day_Zach/MON; Waived Reese_Poke +y/BOS<br>Scott - Activated Hernandez_Runelvys/KCA; Waived Lowe_Derek/ +BOS<br>Scott - Activated VanBenschoten_John/PIT; Waived Quantrill_Pau +l/NYA<br>Scott - Activated Williamson_Scott/BOS; Waived Reyes_Dennys/ +KCA<br>Sean - Activated Burnett_Sean/P

Obviously, this is a parial HTML listing from the web page. I am trying to pull the names in the HTML so I can do some other things to them. Using the following perl, I am able to use HTML::parser and my vague understanding of it to print out what I would like. My questions are: 1) can I have the print statement in the subroutine "text" directed into a file or into an array for use outside of the package? 2) is there another way that I can approach this problem that I am completely missing other than HTML::Parser?
#!d:/perl56/bin use strict; { package HTMLParser; use base "HTML::Parser"; sub text { my ($self, $origtext) = @_; $origtext =~ s/&nbsp;//g; $self->{TEXT} .= $origtext; ### Read through hash and test each line against array @field = qw (C 1B 2B 3B SS OF1 OF2 OF3 DH B1 B2 B3 B4 B5 B6 B7 + B8 SP1 SP2 SP3 SP4 SP5 SW1 SW2 SW3 SW4 SW5 SW6 SW7 RP1 RP2 RP3 RP4 R +P5 RP6 RP7 IR); my @test = split(/\s*[ ]/,$origtext); foreach $line (@field) { if ($test[0] eq $line) { print "$test[1]\n"; } } } } my $leaguefile = $ARGV[0]; open(DATAFILE1,"$leaguefile") || die "Cannot open test file"; my $sp = new HTMLParser; while (<DATAFILE1>) { $sp->parse_file($_); } $sp->eof; close DATAFILE1;

Replies are listed 'Best First'.
Re: html::parser question
by jpeg (Chaplain) on Mar 04, 2005 at 16:36 UTC
    My questions are:

    1) can I have the print statement in the subroutine "text" directed into a file or into an array for use outside of the package?
    2) is there another way that I can approach this problem that I am completely missing other than HTML::Parser?

    1) Sure you can. If you'd like to unshift/push $test1 onto an array, define a global array and pass it to test() as a reference and and return the reference. Or create an array within test() and pass an array reference back.
    If you'd like to print into a file, that's certaily doable too. Open the output file, say print FH "$test[1]\n"; and voila!

    2)I'm not sure. If you have something with a lot of HTML and you want text, then you have to parse it with something. WWW::Mech has a good reputation. HTML::TokeParser's pretty nice too.

    It's kind of a drag that your sample data didn't contain anything that you're searcing for in @field. I assume you're searching for the three capital letters after the /, correct? Maybe you could just split the raw HTML on those.

Re: html::parser question
by Popcorn Dave (Abbot) on Mar 04, 2005 at 16:54 UTC
    Definitely take a look at HTML::TokeParser. Since you're parsing information in to tokens, you can easily pick out the bits that you want.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.