Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Weather warnings from www.meteoalarm.eu

by wfsp (Abbot)
on May 30, 2010 at 14:45 UTC ( #842266=note: print w/replies, xml ) Need Help??


in reply to Re^2: Weather warnings from www.meteoalarm.eu
in thread Weather warnings from www.meteoalarm.eu

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 (&nbsp;) that are scrattered librally throughout the source.

Replies are listed 'Best First'.
Re^4: Weather warnings from www.meteoalarm.eu
by walto (Pilgrim) on Jun 03, 2010 at 14:03 UTC
    Thanks wfsp for your code. I added your suggestion (slightly modified) to the original script.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-10-17 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (232 votes). Check out past polls.

    Notices?