存储问题:一切顺利,但脚本不存储在预定的位置

时间:2012-03-28 21:44:28

标签: perl firefox mechanize www-mechanize storage

对我来说Perl有时看起来abits Abracadabra 非常感谢我对我的耐心......

更新; joe要求我发布不起作用的完整脚本:这里我们有一些代码,我在小怪回答后尝试过(见下文)

严重错误

martin@linux-wyee:~/perl> perl test_8.pl
syntax error at test_8.pl line 25, near ")
 binmode"
Global symbol "$out" requires explicit package name at test_8.pl line 25.
Global symbol "$out" requires explicit package name at test_8.pl line 26.
Execution of test_8.pl aborted due to compilation errors.
martin@linux-wyee:~/perl> su -

这里是我目前运行的脚本......

#!/usr/bin/perl
use strict;
use warnings;

use WWW::Mechanize::Firefox;

my $mech = new WWW::Mechanize::Firefox();

open my $urls, '<', 'urls.txt' or die $!;

while (<$urls>) {
  chomp;
  next unless /^http/i;
  print "$_\n";
  $mech->get($_);
  my $png = $mech->content_as_png;
  my $name = $_;
  $name =~ s#^http://##i;
  $name =~ s#/##g;
  $name =~ s/\s+\z//;
  $name =~ s/\A\s+//;
  $name =~ s/^www\.//;
  $name .= ".png";
 open(my $out, '>', "/images/$name")
 binmode $out;
  print $out $png;
  close $out;
  sleep 5;
}

这里有五个示例网址

http://www.unifr.ch/sfm
http://www.zug.phz.ch
http://www.schwyz.phz.ch
http://www.luzern.phz.ch
http://www.schwyz.phz.ch
http://www.phvs.ch

更新结束;在这里原始的初始线程继续......

我需要从网站上获得一些缩略图但我尝试使用wget - 但这对我不起作用,因为我需要 一些渲染功能needet:我有一个包含2,500个URL的列表,每行一个,保存在文件中。然后我想要一个脚本 - 见下面 - 打开文件,读取一行,然后检索网站并将图像保存为一个小缩略图。 好吧,因为我有一堆网站(2500)我必须决定结果的命名。

http://www.unifr.ch/sfm
http://www.zug.phz.ch
http://www.schwyz.phz.ch
http://www.luzern.phz.ch
http://www.schwyz.phz.ch
http://www.phvs.ch
http://www.phtg.ch
http://www.phsg.ch
http://www.phsh.ch
http://www.phr.ch
http://www.hepfr.ch/
http://www.phbern.ch

到目前为止一切都那么好,我觉得我尝试这样的事情

#!/usr/bin/perl
use strict;
use warnings;

use WWW::Mechanize::Firefox;

my $mech = new WWW::Mechanize::Firefox();

open my $urls, '<', 'urls.txt' or die $!;

while (<$urls>) {
  chomp;
  next unless /^http/i;
  print "$_\n";
  $mech->get($_);
  my $png = $mech->content_as_png;
  my $name = $_;
  $name =~ s#^http://##i;
  $name =~ s#/##g;
  $name =~ s/\s+\z//;
  $name =~ s/\A\s+//;
  $name =~ s/^www\.//;
  $name .= ".png";
  open my $out, ">", $ "images" or die $!;
  binmode $out;
  print $out $png;
  close $out;
  sleep 5;
}

运行一个小脚本并收集/获取结果......我以缩略图的形式收集图像。 到现在为止还挺好。

注意:一切都很好,到目前为止一直运行良好 - 是的,直到我 试图创建一个特殊选项:我想强制脚本将结果存储在文件夹

那么,您如何看待将结果存储在名为images的文件夹中的想法??)这是可行的吗?它会帮助很多,因为我将结果存储在一个文件夹中。 而且很多结果都不会弄乱机器......

我遇到了一些问题。试图这样做 - 将它存储在一个目录中:

open(my $out, '>', "path/$name") or die $!;我是这样做的。

注意 - 名为images 的目录位于同一个文件夹中......

我得到了结果

  perl test_8.pl

Global symbol "$images" requires explicit package name at test_8.pl line 23.
Execution of test_8.pl aborted due to compilation errors.
martin@linux-wyee:~/perl> 
martin@linux-wyee:~/perl> perl test_8.pl
Bareword found where operator expected at test_8.pl line 23, near "$/images"
        (Missing operator before images?)
syntax error at test_8.pl line 23, near "$/images "
Global symbol "$out" requires explicit package name at test_8.pl line 24.
Execution of test_8.pl aborted due to compilation errors.
martin@linux-wyee:~/perl> perl test_8.pl
Bareword found where operator expected at test_8.pl line 23, near "$/images"
        (Missing operator before images?)
syntax error at test_8.pl line 23, near "$/images "
Global symbol "$out" requires explicit package name at test_8.pl line 24.
Execution of test_8.pl aborted due to compilation errors.
martin@linux-wyee:~/perl> 
martin@linux-wyee:~/perl> perl test_8.pl
Bareword found where operator expected at test_8.pl line 23, near "$ "images"
        (Missing operator before images?)
String found where operator expected at test_8.pl line 23, at end of line
        (Missing semicolon on previous line?)
syntax error at test_8.pl line 23, near "$ "images"
Can't find string terminator '"' anywhere before EOF at test_8.pl line 23.
martin@linux-wyee:~/perl> 

1 个答案:

答案 0 :(得分:2)

您的原始帖子包含代码

open(my $out, '>', "path/$name")

这是非常正确的轨道。要在$name目录中编写名称包含在images中的文件,正确的语法是

open(my $out, '>', "images/$name")

我不确定你是如何偏离轨道并尝试$images$/images$ images