Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by walto (Pilgrim)
on May 30, 2010 at 13:21 UTC ( #842262=note: print w/replies, xml ) Need Help??


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

Here it is: http://www.meteoalarm.eu/index3.php?area=DE002&day=0&lang=
  • Comment on Re^2: Weather warnings from www.meteoalarm.eu

Replies are listed 'Best First'.
Re^3: Weather warnings from www.meteoalarm.eu
by wfsp (Abbot) on May 30, 2010 at 14:45 UTC
    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.
      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://842262]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-11-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (282 votes). Check out past polls.

    Notices?