0

好吧,我运行这个脚本,它是为了做一些网站的截图而写的,我也启动并运行 mozrepl

在这里,我们有一些请求的 url 的文件......注意这只是真实列表的一小段 - 真实列表要长得多。它包含超过 3500 行和 URL

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
http://www.ph-solothurn.ch
http://www.pfh-gr.ch
http://www.ma-shp.luzern.phz.ch
http://www.heilpaedagogik.phbern.ch/

奇怪的是输出 - 见下文......问题:我应该更改脚本吗

为什么我要使用以下小脚本获取输出:

#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize::Firefox;

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

open(INPUT, "<urls.txt") or die $!;

while (<INPUT>) {
        chomp;
        print "$_\n";
        $mech->get($_);
        my $png = $mech->content_as_png();
        my $name = "$_";
        $name =~s/^www\.//;
        $name .= ".png";
        open(OUTPUT, ">$name");
        print OUTPUT $png;
        sleep (5);
}

在这里看到压倒性的输出 - 坦率地说,我从来没有想过得到如此有趣的输出..我必须调试整个代码....见下文,

http://www.unifr.ch/sfm
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 2.
http://www.zug.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 3.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 4.
http://www.luzern.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 5.
http://www.schwyz.phz.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 6.
http://www.phvs.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 7.
http://www.phtg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 8.
http://www.phsg.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 9.
http://www.phsh.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 10.
http://www.phr.ch
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 11.
http://www.hepfr.ch/
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 12.
http://www.phbern.ch                                                                                                                                                                  

一些思考:嗯 - 首先,我认为这不是一个非常严重的错误 - 我认为我必须调试它,然后它会更好地工作。其次,我首先认为脚本似乎“使机器过载”?现在我对此不太确定:症状确实看起来很奇怪,但我想没有必要得出“机器过载”的结论

第三,我认为必须采取某些步骤来确保问题与 WWW::Mechanize::Firefox 完全相关?这让我明白 Perl 警告的含义以及使用诊断编译指示获得更多解释的想法:你怎么看?

print() on unopened filehandle FH at -e line 1 (#2) (W unopened) An I/O operation was attempted on a filehandle that w +as never initialized. 

首先- 我们需要进行 open()、sysopen() 或 so +cket() 调用,或者从 FileHandle 包中调用构造函数 - 或者,关闭文件句柄 OUTPUT 上的 print() 也会给出很多答案这将告诉我们,我们没有使用 autodie,也没有检查 open 的返回值。最重要的是我必须调试它并确保找到错误发生的地方[/QUOTE]

但经过一番思考后,我认为值得仔细研究所有测试的东西——你怎么看这个想法总是在使用之前测试以确保文件是打开的。这意味着我们也应该进入使用三个的习惯

arg open():

open my $fh, '>', $name or die "Can't open file $name : $!";
print $fh $stuff;

好吧 - 我想我们可以或应该在不使用的情况下解决这个问题die(),但我们必须手动使用一些方法让我们知道哪些文件无法创建。在我们的例子中,它们看起来都......上面显示的......

更新选择一个好的文件名意味着我需要一个文件名来存储图像。注意:我想将它们全部存储在本地。但是,如果我有一个巨大的 url 列表,那么我会得到一个巨大的输出文件列表。因此我需要有好的文件名。我们可以在节目中反映这些东西和需求吗!?

最新的更新;机械化似乎有一些错误....我想是的!

4

1 回答 1

2

我想打印出二进制数据(jpg 文件),你必须明确地设置它。其次,如果您不再需要文件处理程序并且您在打开时“或死”,请关闭它。第三选择一个好的文件名。

问候,

http://perldoc.perl.org/functions/binmode.html

#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize::Firefox;

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

open(INPUT, "<urls.txt") or die $!;

while (<INPUT>) {
        chomp;
        next if $_ =~ m/http/i;
        print "$_\n";
        $mech->get($_);
        my $png = $mech->content_as_png();
        my $name = "$_";
        $name =~s#http://##is;
        $name =~s#/##gis;$name =~s#\s+\z##is;$name =~s#\A\s+##is;
        $name =~s/^www\.//;
        $name .= ".png";
        open(my $out, ">",$name) or die $!;
        binmode($out);
        print $out $png;
        close($out);
        sleep (5);
}
于 2012-03-27T06:57:00.360 回答