perl - 使用 Mozrepl 使用 WWW::Mechanize::FireFox 创建缩略图 - 一些调试尝试

标签 perl parsing firefox-addon mechanize mozrepl

好吧,我运行这个脚本,它是为了做一些网站截图而编写的
我也启动并运行了 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 列表,那么我会得到一个很大的列表 输出文件 .因此我需要有好的文件名。我们可以吗反射(reflect)那些事情和需要在程序中!?

最新更新 ;似乎有一些 Mechanize 错误 .... 大概吧!!!

最佳答案

如果你想打印二进制数据(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);
}

关于perl - 使用 Mozrepl 使用 WWW::Mechanize::FireFox 创建缩略图 - 一些调试尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884518/

相关文章:

python - 如何解析包含 url 的字符串,将其更改为正确的链接

c++ - 解析 C++ 的复杂性

regex - 使用 golang 解析 Perl 正则表达式

html - 如何用对 sql 数据库内容的查询替换 perl 中的数组?

ruby - 用 Ruby 编写 irssi 脚本

linux - Arch Linux - 如何通过命令行静默安装 Firefox 扩展(没有 install.rdf 文件)

javascript - 允许内容文档(网页)检测我的 Firefox 插件

windows - 如何使用 Perl 在 Windows 中获取没有文件名的路径?

java - jackson JSON 库中的 ALLOW_UNQUOTED_FIELD_NAMES

javascript - Firefox 插件 - 我可以监听 nsIPromptService 处理的提示吗?