I decided to use your post as an opportunity for myself to learn HTML::Parser.

Being my first time at this module, it took ~2 hours to write, with the last 10% of the code
taking 90% of the time...

( FYI: that was in getting the <a> tags within the <td> tags to parse correctly )

So, here ya go -- using HTML::Parser and LWP::UserAgent :

#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTML::Parser; my ( $href, $ua, $req, $resp, $tmp, $i, $p, $tr, @stats ); $href = "http://setiathome.ssl.berkeley.edu/stats/country_7.html"; $ua = LWP::UserAgent->new(); $req = new HTTP::Request('GET', $href); $resp = $ua->request($req); sub get_table_text { return unless $i < 3; my $self = shift; my $text = shift; $self->handler( text => sub { return if shift eq "" }, "dtext" ); ( $text = $text ) =~ s/^\d+\)\s(.*)$/$1/; if ( $i == 1 ) { $tmp .= $text; } elsif ( $i == 2 ) { chomp ($tmp .= ":$text") } } sub grab_href_text { my $self = shift; $self->handler( text => sub { return if shift eq "" }, "dtext" ); $tmp .= shift; } sub end_table { return unless shift eq "tr"; push(@stats, "$tmp\n"); undef $tmp; $i = 0; } sub start { my ( $tag, $self ) = @_; return unless $tag =~ /^(tr|td|a)$/; $tag =~ /td/ && do { $i++; $self->handler( text => \&get_table_text, "self, dtext" ); }; $tag =~ /a/ && do { $self->handler( text => \&grab_href_text, "self, dtext" ) }; $self->handler( end => \&end_table, "tagname, self"); } $p = HTML::Parser->new( api_version => 3 ); $p->handler( start => \&start, "tagname, self" ); $p->parse( $resp->{'_content'} ); print @stats;


Hope that's educational/usefull ... despite the conspicuous lack of comments!

(c8=


In reply to RE: Sneeky Snake by Zarathustra
in thread Sneeky Snake by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.