[英]create thumbnails with WWW::Mechanize::FireFox using Mozrepl - some debug attempts
[英]WWW::Mechanize::Firefox runs well: some attempts to make the script a bit more robust
友人
我有一個不錯的腳本,可以用作圖像抓取工具。 對於最初的試驗和測試,一切進行順利。
這是我針對腳本運行的urls.txt中使用的URL列表。 請注意,這只是一個簡短列表。 我需要針對2500個URL運行,因此,如果腳本更加健壯,那就太好了;如果某些URL不可用或花費太多時間,它將繼續運行。 我認為,如果某些URL不可用或花費太多時間或阻止mozrepl且WWW:Mechanize :: Firefox需要太多時間,該腳本就會遇到一些問題。
您是否認為我的想法和建議可能是造成此問題的原因? 如果是這樣,我們如何才能改善腳本並使它變得更強大,更強大和更健壯 ,以使它不會很快停止。
期待您的來信。
問候。
http://www.bez-zofingen.ch
http://www.schulesins.ch
http://www.schulen-turgi.ch/pages/bezirksschule/startseite.php
http://www.schinznach-dorf.ch
http://www.schule-seengen.ch
http://www.gilgenberg.ch/schule/bez/2005-06/
http://www.rheinfelden-schulen.ch/bezirksschule/
http://www.bezmuri.ch
http://www.moehlin.ch/schulen/
http://www.schule-mewo.ch
http://www.bez-frick.ch
http://www.bezendingen.ch
http://www.bezbrugg.ch
http://www.schule-bremgarten.ch/content/view/20/37/
http://www.bez-balsthal.ch
http://www.schule-baden.ch
http://bezaarau.educanet2.ch/info/.ws_gen/index.htm
http://www.benedict-basel.ch
http://www.institut-beatenberg.ch/
http://www.schulewilchingen.ch
http://www.ksuo.ch
http://www.international-school.ch
http://www.vsgtaegerwilen.ch/
http://www.vgk.ch/
http://www.vstb.ch
但是我想如果它比現在更強大,我會感到非常高興。
當然,它正在像WWW :: Mechanize :: Firefox一樣驅動真正的瀏覽器
因此,在某處可能有些不穩定 ,可能比其他任何屏幕抓取解決方案都更不穩定 。 我收到類似以下的錯誤...(請參閱下文)
請注意,我還仔細查看了Firefox故障排除中的調試頁面,其中提供了有關各種錯誤,麻煩以及類似問題的提示和技巧以及變通辦法。
#!/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, '>', "/home/martin/images/$name") or die $!;
binmode $out;
print $out $png;
close $out;
sleep 5;
}
查看結果以及錯誤停止的地方 。
martin@linux-wyee:~/perl> perl test_10.pl
http://www.bez-zofingen.ch
Datei oder Verzeichnis nicht gefunden at test_10.pl line 24, <$URLs> line 3.
martin@linux-wyee:~/perl> perl test_10.pl
http://www.bez-zofingen.ch
http://www.schulesins.ch
http://www.schulen-turgi.ch/pages/bezirksschule/startseite.php
http://www.schinznach-dorf.ch
http://www.schule-seengen.ch
http://www.gilgenberg.ch/schule/bez/2005-06/
http://www.rheinfelden-schulen.ch/bezirksschule/
Not Found at test_10.pl line 15
martin@linux-wyee:~/perl>
你有什么建議? 我們如何使腳本更健壯? 如何獲得它以便它不會這么早停止?
在異常處理程序中包裝所有可能出錯的方法/系統調用。 (有關該主題的討論,請參見Perl Best Practices第13章。)為Mozrepl設置顯式超時。
遇到錯誤時,將其記錄下來,然后跳到下一個URL。 運行完成后,檢查日志文件,並使用之前無法處理的URL重復運行。 整理出永久不可用的頁面的URL。 最后,由於某些原因,可能會留下一些無法通過Mozrepl進行截圖的頁面。 手動處理。
您應該始終檢查您的響應是否成功。 您更正的代碼:
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";
my $res = $mech->get($_);
if(!$res->is_success()){
next; # or continue;
}
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, '>', "/home/martin/images/$name") or die $!;
binmode $out;
print $out $png;
close $out;
sleep 5;
}
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.