【发布时间】:2012-03-31 00:50:55
【问题描述】:
查看详细帖子末尾的更新
- 好吧,我有点困惑,我不得不承认......;-) 作为一个新手 perl 朋友,perl 代码总是看起来有点 abracadaba......
我需要一些来自网站的缩略图,但我尝试使用 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
到目前为止 很好,我想我试试这样的东西
#!/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;
}
我现在得到以下结果.... 看看会出现什么... 据我所知 - 文件夹“images”中没有存储图像
为什么不!?
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>
输出想对我说什么... 我现在能做什么!?
更新
亲爱的你好
感谢回复 - 猜我这里有权限问题....
我有这个……
#!/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;
}
这行得通 - 但我能得到的只是存储到 test_8.pl 所在的目录中
猜这是一个权限问题。
我能做什么。
我可以将图像目录放在 perl.. 文件夹之外的某个位置吗? 也许我已经创建了
perl 目录或 具有特殊 root 权限的图像目录。
到目前为止,我为解决这些问题所做的是
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 #
图片文件夹为空
我能做什么
我可以在 perl 目录之外创建一个图像文件夹
如何命名它的字符串路径..?!
亲爱的伙伴 - 我们几乎都在那里 - 我很确定 - 我想这只是一个权限问题。但是怎么解决呢!?
也许我必须再次在一个全新的目录中创建所有测试文件。不是root,而是普通用户!?你说什么!?
【问题讨论】:
-
声明
open(my $out, '>', "/images/$name")似乎失败添加or die $!进行故障排除。 -
你好 .- 查看初始帖子中的更新 - 猜你是对的
标签: perl filehandle