Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re^3: Weather warnings from by wfsp
in thread Weather warnings from by walto

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2019-11-17 12:31 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (86 votes). Check out past polls.