Another take on _parse_details
#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use HTML::TreeBuilder;
# meteoalarm.html contains the source of your link
open my $fh, q{<}, q{meteoalarm.html}
or die qq{cant open file to read: $!\n};
my $content = do{local $/; <$fh>};
my $data = _parse_details($content);
print Dumper $data;
sub _parse_details {
my $content =shift;
my (%data);
my $p = HTML::TreeBuilder->new_from_content(
$content
);
$data{fullname} = $p->look_down(
_tag => q{h1}
)->as_text;
my @warnbox_divs = $p->look_down(
_tag => q{div},
class => qr/warnbox wb\d/
);
for my $div (@warnbox_divs) {
my ($as_txt);
my @info_divs = $div->look_down(
_tag => q{div},
class => q{info}
);
$as_txt = $info_divs[0]->as_text;
my ($from, $until) =
$as_txt =~ /valid from (.*) Until (.*)$/;
$as_txt = $info_divs[1]->as_text;
my ($warning, $level) =
$as_txt =~ /([^\s]+)\s+Awareness Level:\s+(.*)/;
my $text = $div->look_down(
_tag => q{div},
class => q{text}
)->as_text;
$data{warnings}{$warning} = {
level => $level,
from => $from,
until => $until,
text => $text,
};
}
return \%data;
}
Output. I shortened the 'text' value for clarity.
$VAR1 = {
'warnings' => {
'Wind ' => {
'until' => '30.05.2010 22:00 CET ',
'level' => 'Yellow ',
'text' => 'Allm㧬ich zunehmender S�wind mit .....',
'from' => '30.05.2010 06:05 CET'
},
'Rain ' => {
'until' => '01.06.2010 10:00 CET ',
'level' => 'Orange ',
'text' => 'Zeitweise schauerartig verst㱫ter Dauerregen ....',
'from' => '30.05.2010 07:17 CET'
},
'Thunderstorms ' => {
'until' => '30.05.2010 15:00 CET ',
'level' => 'Yellow ',
'text' => 'Loklae Gewitter. Dabei vereinzelt Sturmb�bis ....',
'from' => '30.05.2010 13:30 CET'
}
},
'fullname' => 'Weather warnings: Baden-Württemberg '
};
As you can see, you have to be aware you're handling UTF8 (which, because it uses HTML::Entities) is what HTML::TreeBuilder returns). Beware of decoding anything twice. And don't trip up on the non breaking spaces ( ) that are scrattered librally throughout the source.
|