perl-mechanize有限制 - 几个调试尝试开始 [英] perl-mechanize runs into limitations - several debugging attempts started

查看:198
本文介绍了perl-mechanize有限制 - 几个调试尝试开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好亲爱的开发人员。

首先 - 对于新手而言抱歉,我对Perl很新。

first of all - sorry for being the newbie.. i am pretty new to Perl.

我正在尝试学习一些关于perl的东西,同时使用代码和代码片段。
今天我有一个脚本运行机械化的工作..但有些不运行到最后。 Waht的目标是:我想获得一些wesite-sceenshots的缩略图。

i am trying to learn something about perl while playin around with code - and snippets. Today i have a little script that runs a mechanize job.. but somewhat does not run to the end. Waht is aimed: i want to get some thumbnails of wesite-sceenshots.

我运行这个脚本,这是为了做一些网站截图,我也已经运行mozrepl。什么奇怪的是输出 - 见下面的问题:我应该改变脚本为什么我输出?

well i run this script , which is written to do some screenshots of websites i have also up and running mozrepl. whats strange is the output - see below... question: should i do change the script why do i ge the output?

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

代码给badck的是

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

已经做了这样的票价来摆脱这个问题:
我可以使用诊断语法来获取更多的见解发生了什么...
或者,打开()关闭文件句柄OUTPUT还给了我们很多答案,
会告诉我们,我们没有使用autodie,也没有检查打开的返回值。

what i have doen so fare to get rid of the issues: well i can use the diagnostics-pragma to get more insights into what is happening... Alternatively, print() on closed filehandle OUTPUT also gives us lots of answers that will tell us that we did not use autodie and also did not check the return value of open.

hmmm - well我只是在文件句柄上轻松地

hmmm - well i just mused on the filehandle

好:打开的电话失败,既然你认为它是成功的,并继续尝试使用文件句柄(没有打开),你收到该错误。

well: the open call failed and since you assumed it was successful and proceeded to attempt to use the filehandle (which was not opened), you received that error.

这里要学习的教训是,我们应该始终检查一个打开的呼叫的返回码,以验证它是否成功,并采取适当的措施,如果它是't。

The lesson here to learn is that we should ALWAYS check the return code of an open call to verify that it was successful and take proper action if it wasn't.

好吧 - 我想我必须在这里学习一些perl问题...我想我必须相应地纠正代码。

well - i guess that i have to learn here some perl-issues... I guess that i have to correct the code accordingly.

我们还应该注意,应该使用3个arg形式的open和一个词法var作为文件句柄。

we should also take care and should use the 3 arg form of open and a lexical var for the filehandle.

嗯这个在这里呢
代码:

hmm what about this one here. Code:

open my $out_fh, '>', $name or die "failed to create/open '$name' <$!>";

我只是可以将这部分构建到原始代码中。whatcha认为?

I just could build this part into the original code.. whatcha think?

#!/usr/bin/perl




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

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

open my $out_fh, '>', $name or die "failed to create/open '$name' <$!>";


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);
}


你觉得怎么样?

well what do you think?

如何更改代码 - 并确保脚本将成功运行...

how would you change the code - and make sure that the script will run successfully...

推荐答案

一方面,您的输入包含斜杠,然后您尝试使用该输入创建文件名。由于您的输入以 http:// www 而不是 www 开头,您的替代操作不会也可以做任何事情。

For one thing, your input contains slashes and then you are trying to use that input to create a filename. Since your input begins with "http://www" and not "www", your substitution operation doesn't do anything, either.

my $name = "$_";            # e.g. $name <= "http://www.zug.phz.ch"
$name =~s/^www\.//;         # $name still is "http://www.zug.phz.ch"
$name .= ".png";            # $name is ""http://www.zug.phz.ch.png"
open(OUTPUT, ">$name");     # error: no directory named "./http:"
print OUTPUT $png;
sleep (5);

做一个更好的清理文件名的工作,也许像

You'll want to do a better job of sanitizing your filename. Maybe something like

$name =~ s![:/]+!-!g; #http://foo.com/bar.html  becomes  http-foo.com-bar.html

如果有的话,您返回的值将在中调用,而循环。如果你说过

And if anything, you return value you want to check is in the open call inside your while loop. If you had said

open(OUTPUT,">$name") or warn "Failed to open '$name': $!";

你可能会自己弄清楚这一点。

you probably would have figured this out on your own.

这篇关于perl-mechanize有限制 - 几个调试尝试开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆