#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |