http://www.perlmonks.org?node_id=507207

mikey19661 has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to incorporate images from: http://weather.yahoo.com/forecast/USCA0639_c.html?force_units=1 into my site. Yet I don't know exactly how to grab the images out of there, for I don't have much experience with perl. Could anyone point me to a location where I can learn more on how to do this, or are there any modules that can grab these images for me?

Replies are listed 'Best First'.
Re: Weather RSS image
by InfiniteSilence (Curate) on Nov 09, 2005 at 21:15 UTC
    Uh, I don't think you want to grab images from someone else's site to incorporate in your own. That kind of thing typically incites a 'cease and desist' letter from an unfriendly legal department.

    I think what you want is their RSS Feed (technically you are not supposed to re-use that either, but don't tell anybody I told you). Just use XML::Simple to parse it for what you are looking for and show your own rain images.

    Celebrate Intellectual Diversity

Re: Weather RSS image
by valdez (Monsignor) on Nov 09, 2005 at 21:32 UTC

    Welcome at the Monastery!
    There is a better way to do this: instead of grabbing that page, build your own page using data got via RSS! Grab the data available from one of the sources listed at Yahoo RSS using XML::RSS::Feed and build the page you want with HTML::Template. Have fun!

    Ciao, Valerio

Re: Weather RSS image
by pg (Canon) on Nov 09, 2005 at 21:46 UTC

    Looks like there are only 48 images there, and the following code should grab all of them for you.

    use LWP::UserAgent; use strict; use warnings; my $ua = LWP::UserAgent->new(); for my $index (0 .. 47, 3200) { print "downloading $index.gif\n"; my $res = $ua->get("http://us.i1.yimg.com/us.yimg.com/i/us/we/52/$ +index.gif"); if ($res->is_success) { open GIF, ">", "weather$index.gif"; binmode(GIF); print GIF $res->content; close(GIF); } }

    Update: Added 3200.gif, as ikegami pointed out.

      (above caveats on harvesting the images) but just for sake of example here's a bash one liner:
      for n in `seq 0 99` ; do wget http://us.i1.yimg.com/us.yimg.com/i/us +/we/52/$n.gif ; done
      Also, instead of get() and open/print/close, can just use LWP::Simple's getstore() method:
      use LWP::Simple; use strict; use warnings; for my $index (0 .. 99) {# actually 47 is the last index print "downloading $index.gif\n"; my $res = getstore("http://us.i1.yimg.com/us.yimg.com/i/us/we/52/$ +index.gif", "weather$index.gif"); }
      or just:
      perl -MLWP::Simple -e 'getstore("http://us.i1.yimg.com/us.yimg.com/i/u +s/we/52/$_.gif", "weather$_.gif") for 0 .. 99'

        Unfortunately your Perl code does not work. I called binmode() in my code for a reason. Your code would be fine with html files, but with images, it does not work.

        If you test your code, you will see that some images are broken (or cannot be recognized as the right format).