Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^3: Weather warnings from

by wfsp (Abbot)
on May 28, 2010 at 08:50 UTC ( #842005=note: print w/replies, xml ) Need Help??

in reply to Re^2: Weather warnings from
in thread Weather warnings from

Ok, from looking at the link we can simplify things enourmously.

The data we are after are in cells with class col1 or col2. We can loop over those and extract what we need. You will need to tweak as appropriate but hopefully it will give you the idea.

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; # meteoalarm.html is the source from the website open my $fh, q{<}, q{meteoalarm.html} or die qq{cant open file to read: $!\n}; my $content = do{local $/; <$fh>}; my $mp = Meteoalarm::Parser->new($content); my $data = $mp->parse; print Dumper $data; package Meteoalarm::Parser; use HTML::TreeBuilder; use Data::Dumper; sub new { my $class = shift; my $content = shift; my $p = HTML::TreeBuilder->new_from_content($content); my $self = { parser => $p, }; bless($self, $class); return $self; } sub parse { my $self = shift; my $p = $self->{parser}; my (%data); my @cells = $p->look_down(_tag => q{td}, class => qr/^col[12]$/); for my $cell (@cells){ my $div = $cell->look_down(_tag => q{div}); my $id = $div->id; my $alt = $div->attr(q{alt}); my $img = $div->look_down(_tag => q{img}); my $src = $img?$img->attr(q{src}):q{}; $data{$id}{fullname} = $alt; $data{$id}{warning} = $src; } return \%data; }
output (extract)
$VAR1 = { 'UK' => { 'warning' => '', 'fullname' => 'United Kingdom' }, 'CY' => { 'warning' => '', 'fullname' => 'Cyprus' }, 'IE' => { 'warning' => 'Bilder/wf/wf_23.jpg', 'fullname' => 'Ireland' }, 'IS' => { 'warning' => '', 'fullname' => 'Iceland' }, 'NL' => { 'warning' => '', 'fullname' => 'Netherlands' }, 'BE' => { 'warning' => '', 'fullname' => 'Belgium' }, 'AT' => { 'warning' => 'Bilder/wf/wf_23.jpg', 'fullname' => 'Austria' }, };

Replies are listed 'Best First'.
Re^4: Weather warnings from
by walto (Pilgrim) on May 30, 2010 at 12:28 UTC
    Thanks wfsp for your very helpful posts. With your advice I was able to change the script. I updated the original code with the new one.
    The html for country and region warnings differ slightly. So I kept the original structure with different methods in subs.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://842005]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-12-15 20:36 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (443 votes). Check out past polls.