簡體   English   中英

使用Mozrepl使用WWW :: Mechanize :: FireFox創建縮略圖 - 一些調試嘗試

[英]create thumbnails with WWW::Mechanize::FireFox using Mozrepl - some debug attempts

好吧,我運行這個腳本,這是為了做一些我已經運行mozrepl的網站的截圖

這里我們有一些請求的URL文件...請注意,這只是真實列表的一小段 - 真正的列表要長得多。 它包含超過3500行和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
http://www.phtg.ch
http://www.phsg.ch
http://www.phsh.ch
http://www.phr.ch
http://www.hepfr.ch/
http://www.phbern.ch
http://www.ph-solothurn.ch
http://www.pfh-gr.ch
http://www.ma-shp.luzern.phz.ch
http://www.heilpaedagogik.phbern.ch/

什么是奇怪的輸出 - 見下文...問題:我應該更改腳本

為什么我使用以下小腳本輸出:

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

看到這里壓倒性的輸出 - 坦率地說,我從來沒有得到如此有趣的輸出..我必須調試整個代碼....見下文,

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
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 11.
http://www.hepfr.ch/
print() on closed filehandle OUTPUT at test_3.pl line 20, <INPUT> line 12.
http://www.phbern.ch                                                                                                                                                                  

一些想法:首先,我認為這不是一個非常嚴重的錯誤 - 我認為我必須調試它然后它會更好地工作。 其次,我首先認為腳本似乎“超載機器”? 現在我不是很確定:症狀確實看起來很奇怪,但我想結束“機器超載”並不是必要的

第三,我認為必須采取某些步驟來確保問題與WWW :: Mechanize :: Firefox完全相關? 這讓我想到了Perl警告意味着什么,以及使用診斷實用程序獲得更多解釋的想法:你怎么看?

print() on unopened filehandle FH at -e line 1 (#2) (W unopened) An I/O operation was attempted on a filehandle that w +as never initialized. 

首先 - 我們需要做一個open(),一個sysopen()或一個so + cket()調用,或者從FileHandle包中調用一個構造函數 - 或者,封閉文件句柄上的print()OUTPUT也提供了很多答案這將告訴我們,我們沒有使用autodie,也沒有檢查open的返回值。 最重要的是我必須調試它並確保找到錯誤發揮作用[/ QUOTE]

但經過一些思考之后,我認為值得仔細研究一下所有的測試事項 - 你對這個想法有何看法, 總是先測試一下,確保文件在使用之前是開放的。這意味着我們也應該進入使用三者的習慣

arg open():

open my $fh, '>', $name or die "Can't open file $name : $!";
print $fh $stuff;

好吧 - 我想我們可以或者應該在不使用die()情況下解決這個問題,但我們必須手動設置一些方法讓我們知道哪些文件無法創建。 在我們的例子中,看起來像所有這些 ......如上所示......

更新選擇一個好的文件名你的意思是我需要一個文件名來存儲圖像..注意:我想在本地存儲所有這些 但如果我有一個巨大的網址列表,那么我得到一個巨大的輸出文件列表。 因此我需要有良好的文件名。 我們能否在計划中反映這些事情和需求!?

最新的更新 ; 機械化似乎有一些錯誤 ....我想是這樣!!!

我想打印出二進制數據(jpg文件),你必須明確地設置它。 其次,如果您不再需要文件處理程序,請關閉文件處理程序,並在打開時“死”。 第三,選擇一個好的文件名。

問候,

http://perldoc.perl.org/functions/binmode.html

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM