2011-10-18 52 views
3

我试图抓住位于here的图像并每天将其保存在我的服务器中几次,就好像我在图像上“右键单击”并保存它在我的桌面上。我已经决定使用perl脚本来做到这一点,这是我写了这么远:抓取图像from.asp url并保存它

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运行它后,我收到此错误:无法调用“正则表达式”上的行未定义的值2

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

感谢任何帮助。

回答

0

注意,URL显示了在浏览器中意思是没有HTML搜索的图像PNG图像。原则上,那么,下面的脚本应该工作:

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

我用了一个类似的脚本来产生Norwegian Sun in the Baltic Sea - 6 days in 5 minutes

+0

这一个就像一个魅力。非常感谢你。 – user991329

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;