Pergunta

I'm trying to grab the image located here and save it in my server few times per day, just as if I "right-click" on the image and save it on my desktop. I have decided to use perl script to do this, here's what I wrote so far:

use Image::Grab;
 $pic->regexp('.*\.png');
 $pic->search_url('http://www.reuters.wallst.com/enhancements/chartapi/index_chart_api.asp?symbol=.SPX&headerType=quote&width=316&height=106&duration=3');
 $pic->grab;
open(IMAGE, ">index_chart_api.png") || die"index_chart_api.png: $!";
 binmode IMAGE;  # for MSDOS derivations.
 print IMAGE $pic->image;
 close IMAGE;

After running it via ssh I receive this error: Can't call method "regexp" on an undefined value at line 2

Anyone has any idea what is wrong with this line "$pic->regexp('.*.png');" or how to grab and save this image (index_chart_api.png) from mentioned url on ones server properly ?

Appreciate any help with this.

Foi útil?

Solução

Note that the URL gave shows the PNG image in my browser meaning there is no HTML to search for the image. In principle, then, the following script ought to work:

#!/usr/bin/env perl

use warnings; use strict;
use LWP::Simple qw(getstore is_error);

my $img_url = 'http://www.reuters.wallst.com/enhancements/chartapi/index_chart_api.asp?symbol=.SPX&headerType=quote&width=316&height=106&duration=3';

my $ret = getstore($img_url, 'test.png');

if (is_error($ret)) {
    die "Error: $ret\n";
}

I used a similar script to produce Norwegian Sun in the Baltic Sea - 6 days in 5 minutes.

Outras dicas

You haven't init the object, that's why it is undefined.

use Image::Grab;
$pic = new Image::Grab;
$pic->regexp('.*\.png');

or similar thing:

use Image::Grab;

$pic = Image::Grab->new(
            SEARCH_URL => '',
            REGEXP     => '.*\.png');
$pic->grab;
open(IMAGE, ">image.jpg") || die "image.jpg: $!";
binmode IMAGE;  
print IMAGE $pic->image;
close IMAGE;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top