binmode() 在关闭的文件句柄 $out 上 ... print() 在关闭的文件句柄 $out 上

问题描述 投票:0回答:1

请参阅详细帖子末尾的更新

  • 好吧,我不得不承认我有点困惑......;-) 作为一个 Perl 新手朋友,Perl 代码总是看起来有点 abracadaba......

我需要一些网站的缩略图,但我尝试使用 wget - 但这对我不起作用,因为我需要一些渲染功能,需要什么:我有一个包含 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/$name");
  binmode $out;
    print $out $png;
    close $out;
    sleep 5;
  }

我现在得到以下结果...... 看看会发生什么... 据我所知 - “图像”文件夹中没有存储任何图像

为什么不!?

rtin@linux-wyee:~> cd perl
martin@linux-wyee:~/perl> perl test_8.pl
http://www.unifr.ch/sfm
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 2.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 2.
http://www.zug.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 3.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 3.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 4.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 4.
http://www.luzern.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 5.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 5.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 6.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 6.
http://www.phvs.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 14.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 14.                                                                                        http://www.pfh-gr.ch                                                                       Got status code 500 at test_8.pl line 15                                                                                         martin@linux-wyee:~/perl>                                                                               

输出想对我说什么...... 我现在能做什么!?

更新

你好亲爱的

感谢您的回复 - 我猜我这里有权限问题......

我有这个...

  #!/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")or die $!;
  binmode $out;
    print $out $png;
    close $out;
    sleep 5;
  }

这可行 - 但我所能得到的只是存储到 test_8.pl 所在的目录中

猜测这是一个权限问题。

我能做什么。

我可以将图像目录放在 perl 文件夹之外的某个地方吗? 也许我已经创建了

perl 目录或 具有特殊 root 权限的图像目录。

到目前为止我为解决问题所做的是

a- 检查文件夹的权限 - 珀尔 。 Perl/图像

b.- 以 root 用户身份在命令行中运行脚本。

我能得到的只是存储在文件夹中的结果,...

linux-wyee:/home/martin/perl_dev/perl # ls
.directory                    images                    module_test         pfh-gr.ch.png  phsg.ch.png          phtg.ch.png  schwyz.phz.ch.png  test_4.pl  test_8.pl        urls.txt
heilpaedagogik.phbern.ch.png  luzern.phz.ch.png         module_test.pl      phbern.ch.png  phsh.ch.png          phvs.ch.png  test_2.pl          test_6.pl  test_8.pl~       zug.phz.ch.png
hepfr.ch.png                  ma-shp.luzern.phz.ch.png  open-local-file.pl  phr.ch.png     ph-solothurn.ch.png  .png         test_3.pl          test_7.pl  unifr.chsfm.png
linux-wyee:/home/martin/perl_dev/perl # 

图像文件夹是空的

我能做什么

我应该在 perl 目录之外创建一个图像文件夹吗

如何命名它的字符串路径..?!

亲爱的朋友 - 我们已经差不多了 - 我很确定 - 我想这只是一个许可问题。 但如何解决!?

也许我必须再次在一个全新的目录中创建所有测试文件。不是作为 root 而是作为普通用户!?你说什么!?

perl filehandle
1个回答
6
投票

您无法打开文件进行写入。您的路径是 /images,您可能没有该目录的权限(如果它存在)。始终检查对 open 调用的返回值,就像您在第一个 open 中所做的那样。

如果我是你,我就不会使用/images。我会将所有内容下载到我控制的目录中,并且不会扰乱标准目录布局。如果您不进行系统管理,则几乎不应该在 / 下创建新目录。

© www.soinside.com 2019 - 2024. All rights reserved.