I'm a big fan of HTML::TokeParser for this sort of data extraction work ...

#!/usr/bin/perl -w use HTML::TokeParser; use strict; my $html = '<a href="/f1/show?page=matchup&lid=206&week=8&mid1=2&mid2= +8">\n'; my @results; my $parser = HTML::TokeParser->new(\$html) || die $!; while (my $token = $parser->get_token) { my $type = shift @{ $token }; if ($type eq "S") { my ($tag, $attr, $attrseq, $text) = @{ $token }; if (($tag eq "a") && ($attr->{'href'} =~ /^\/f1\/show\?/i)) { my $a_href = $attr->{'href'}; $a_href =~ s/.+\?//g; my %vars = map { ((split(/=/, $_))[0]) => ((split(/=/, $_) +)[1]) } split(/&/, $a_href); push (@results, \%vars); }; }; }; foreach my $vars (@results) { print $_, " ", ${$vars}{$_}, "\n" foreach keys %{$vars}; }; exit 0;

This code also checks to ensure that the href of the anchor matches what we want (eg. /f1/show) and allows for multiple matches in the HTML. Also too, if you are to extract this data from live web pages, the assignment of $html can easily be replaced with something similar to this:

use LWP::Simple; my $html = get('http://url.to.webpage.com/'); die "LWP::Simple failed to retrieve source HTML - $!" unless ($html);

There has also been a tutorial written on this module here by crazyinsomniac which includes an excellent step-by-step example for building a program based on HTML::TokeParser.

 

Ooohhh, Rob no beer function well without!


In reply to Re: Re: Extract numbers..... by rob_au
in thread Extract numbers..... 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.