请参阅插入结尾处的更新
我需要从网站获得一些缩略图,但我尝试使用wget - 但这对我不起作用,因为我需要一些渲染功能needet:我有一个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
Run Code Online (Sandbox Code Playgroud)
到目前为止这么好,我想我尝试这样的事情
#!/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;
}
Run Code Online (Sandbox Code Playgroud)
我现在得到以下结果....看看是什么出来...并且据我所知 - 文件夹"图像"中没有存储图像
为什么不!?
rtin@linux-wyee:~> cd perl
martin@linux-wyee:~/perl> perl test_8.pl
http://www.unifr.ch/sfm
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 2.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 2.
http://www.zug.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 3.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 3.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 4.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 4.
http://www.luzern.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 5.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 5.
http://www.schwyz.phz.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 6.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 6.
http://www.phvs.ch
binmode() on closed filehandle $out at test_8.pl line 25, <$urls> line 14.
print() on closed filehandle $out at test_8.pl line 26, <$urls> line 14. http://www.pfh-gr.ch Got status code 500 at test_8.pl line 15 martin@linux-wyee:~/perl>
Run Code Online (Sandbox Code Playgroud)
输出想对我说什么......我现在能做什么!?
更新
亲爱的,亲爱的
thx的回复 - 猜我在这里有一个许可问题....
好吧,我有这个......
#!/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")or die $!;
binmode $out;
print $out $png;
close $out;
sleep 5;
}
Run Code Online (Sandbox Code Playgroud)
这很有用 - 但我能得到的只是存储到test_8.pl所在的目录中
猜测这是一个许可问题.
我能做什么.
我可以将图像目录放在perl文件夹之外的某个位置吗?也许我已经创造了
perl-directory或具有一些特殊root权限的images-directory.
我到目前为止解决问题的方法是
a-检查文件夹的权限 - perl.的Perl /图片
b.-以root用户身份在命令行中运行脚本.
我能得到的是存储在文件夹中的结果,......
linux-wyee:/home/martin/perl_dev/perl # ls
.directory images module_test pfh-gr.ch.png phsg.ch.png phtg.ch.png schwyz.phz.ch.png test_4.pl test_8.pl urls.txt
heilpaedagogik.phbern.ch.png luzern.phz.ch.png module_test.pl phbern.ch.png phsh.ch.png phvs.ch.png test_2.pl test_6.pl test_8.pl~ zug.phz.ch.png
hepfr.ch.png ma-shp.luzern.phz.ch.png open-local-file.pl phr.ch.png ph-solothurn.ch.png .png test_3.pl test_7.pl unifr.chsfm.png
linux-wyee:/home/martin/perl_dev/perl #
Run Code Online (Sandbox Code Playgroud)
images文件夹是空的
我能做什么
我可以在perl目录之外创建一个images文件夹
如何命名它的字符串路径..?!
亲爱的朋友 - 我们一直都在那里 - 我很确定 - 我猜这只是一个许可问题.但是如何解决呢!?
也许我必须再次在一个全新的目录中创建所有的测试文件.不是root用户而是普通用户!?你说什么!?