0

好吧,对我来说,Perl 有时看起来很像 Abracadabra,非常感谢您对我的耐心...

更新; 在 user1269651 和 Bodoin 提供了很好的修复之前有一些错误

查看 bodoins 代码的结果..(请注意,他曾经更改过一次代码 - 我在这里使用了第一个版本... :;

linux-wyee:/home/martin/perl # perl test_7.pl
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                                                                         Use of uninitialized value $png in print at test_7.pl line 25, <$urls> line 10.                                                                                        http://www.phr.ch                                                                          http://www.hepfr.ch/
http://www.phbern.ch
http://www.ph-solothurn.ch
http://www.pfh-gr.ch
Got status code 500 at test_7.pl line 14
linux-wyee:/home/martin/perl # 

和最新版本的 bodins 代码,一些结果看起来像这样..

Can't call method "addProgressListener" on an undefined value at /usr/lib/perl5/site_perl/5.14.2/WWW/Mechanize/Firefox.pm line 566, <$urls> line 12.

好吧,还剩下一些小事情-见上文...我们可以如何处理这些小错误..顺便说一句:将结果存储在文件夹中的想法怎么样。.. /(称为图像左右!?)

更新结束...

这里初始线程开始 - 并给出了想要的大纲:

我需要一些来自网站的缩略图,但我尝试使用 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

到目前为止一切顺利,我想我尝试这样的事情

如果我们不再需要它,我们还必须关闭文件处理程序。除此之外,我们可以在打开时使用“或死”。我做到了-见下文!

顺便说一句,我们需要一个好的文件名。因为我有一个巨大的 url 列表,所以我得到一个巨大的输出文件列表。因此我需要有好的文件名。我们可以在节目中反映这些东西和需求吗!?

脚本根本没有启动....

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

2 回答 2

1

您的代码存在许多问题。最重要的是线

next if $_ =~ m/http/i;

urls.txt它会丢弃包含的所有行http,这不是您想要的。

我没有单独解决每个问题,而是提供了一个功能版本。我希望这是令人满意的。

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, ">", $name or die $!;
  binmode $out;
  print $out $png;
  close $out;
  sleep 5;
}
于 2012-03-27T20:28:00.087 回答
1

我想出了这个:

while (my $name = <DATA>) {
        chomp ($name) ;

        #$mech->get($_);
        #my $png = $mech->content_as_png();
        $name =~ s#http://##;  #REMOVE THIS LINE

        $name =~s#/#-#gis;
        $name =~s#\s+\z##is;$name =~s#\A\s+##is;

        $name =~s/^www\.//;

        $name .= ".png";

        print $name . "\n\n";   #REMOVE THIS LINE       
        #open(my $out, ">",$name) or die $!;
        #binmode($out);
        #print $out $png;
        #close($out);
        #sleep (5);
}


__DATA__
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

您应该能够根据需要对其进行修改,我注释掉了除正则表达式之外的所有内容。我还更改了一个正则表达式,将“/”替换为“-”,从而减少错误生成重复 URL 的可能性。

所以 http://www.unifr.ch/sfm看起来像这样:unifr.ch-sfm

希望这可以帮助

于 2012-03-27T20:47:56.477 回答