3

我试图抓取位于此处的图像并将其保存在我的服务器中,每天几次,就像我“右键单击”图像并将其保存在我的桌面上一样。我决定使用 perl 脚本来执行此操作,这是我到目前为止所写的:

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;

通过 ssh 运行它后,我收到此错误:Can't call method "regexp" on an undefined value at line 2

任何人都知道这行“$pic->regexp('.*.png');”有什么问题 或者如何正确地从服务器上的提到的 url 抓取和保存这个图像(index_chart_api.png)?

感谢您对此的任何帮助。

4

2 回答 2

0

您还没有初始化对象,这就是它未定义的原因。

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

或类似的东西:

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;
于 2011-10-19T01:53:45.267 回答
0

请注意,给出的 URL 在我的浏览器中显示了 PNG 图像,这意味着没有 HTML 来搜索图像。那么,原则上,下面的脚本应该可以工作:

#!/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";
}

我使用类似的脚本在波罗的海制作了挪威的太阳——5 分钟内 6 天

于 2011-10-19T01:56:56.200 回答