0

私にとって、Perl は時々 Abracadabra のように見えます。私との忍耐に感謝します...

アップデート; ジョーは私に、機能していない完全なスクリプトを投稿するように依頼しました: ここに、暴徒の回答後に私が試したコードがあります (以下を参照)

ずっと深刻なエラーが発生しています:

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

ここに5 つのサンプル 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

更新の終了; ここで元の初期スレッドが続きます...

私はウェブサイトからいくつかのサムネイルを持っている必要がありますが、wgetを使用しようとしました-しかし、必要なものをレンダリングする機能が必要なので、それは私にとってはうまくいきません.2,500個のURLのリストがあり、各行に1つずつ、ファイルに保存されています. 次に、スクリプトが必要です-以下を参照してください-ファイルを開き、行を読み取り、Web サイトを取得して画像を小さなサムネイルとして保存します。私はたくさんのウェブサイト (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> 
4

1 に答える 1

2

元の投稿にコードが含まれていました

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

これは非常に正しい方向に進んでいます。$nameディレクトリに名前が含まれているファイルを書き込むためimagesの正しい構文は次のとおりです。

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

どうやって軌道から外れて、、、を試したの$imagesかわかりません。$/images$ images

于 2012-03-28T22:31:25.343 に答える