2013-10-23 208 views
-1

我正在嘗試運行perl腳本(在Windows cmd窗口中),但它會在某個點停止工作。我怎麼知道爲什麼它不會繼續?Perl腳本停止運行

下面是腳本:我可以看到被執行的最後一件事是本着「get_html_source()」 37

#!/usr/bin/perl 
# Perl script that scrapes the members of the Hellenic Parliament 
# Created by Kostas Ntonas, 03 May 2013 - http://ntonas.gr 
# http://deixto.blogspot.gr/2013/05/scraping-members-of-greek-parliament.html 

use strict; 
use warnings; 
use utf8; 

use IO::File; 
use POSIX qw(tmpnam); 
use DEiXToBot; 
use WWW::Selenium; 

my $agent = DEiXToBot->new(); # create the DEiXToBot agent object 

# launch a Firefox instance 
my $sel = WWW::Selenium->new(host => "localhost", 
           port => 4444, 
           browser => "*firefox", 
           browser_url => "http://www.hellenicparliament.gr/" 
          ); 
$sel->start; 

for my $i (1..30) { 

    my $url = "http://www.hellenicparliament.gr/en/Vouleftes/Viografika-Stoicheia?pageNo=$i"; 

    $sel->open($url); 

    $sel->wait_for_page_to_load(5000); 

    $sel->pause(1); 

    print "$i) $url\n"; 

    my $content = $sel->get_html_source(); 

    my ($fh,$name); # create a temporary file containing the page's source code 
    do { $name = tmpnam() } until $fh = IO::File->new($name, O_RDWR|O_CREAT|O_EXCL); 
    binmode($fh, ':utf8'); 
    print $fh $content; 
    close $fh; 

    $agent->get("file://$name"); # load the temporary file/page with the DEiXToBot agent using the file:// scheme 

    unlink $name; # delete the temporary file, it is not needed any more 

    if (! $agent->success) { die "Could not fetch the temp file!\n"; } 

    $agent->build_dom(); 

    $agent->load_pattern('C:\Users\XXX\Documents\Privat\MyCase3\Deixto Patterns\parliament_CVs.xml'); 

    $agent->extract_content(); 

    if (! $agent->hits) { 
     die "Could not find any MPs/ records!\n"; 
    } 
    else { 
     for my $record ($agent->records) { 
      my @rec = @$record; 

      my $party; 
      my $logo = $rec[0]; 

      # deduce the party name from the logo in the first column of the table 
      if ($logo=~m#ND_Logo#) { $party = "N.D. (New Democracy)"; } 
      elsif ($logo=~m#COALITION#) { $party = "SYRIZA Unitary Social Front"; } 
      elsif ($logo=~m#PASOK#) { $party = "PA.SO.K. (Panhellenic Socialist Movement)"; } 
      elsif ($logo=~m#ANEKS_ELL#) { $party = "ANEXARTITOI ELLINES (Independent Hellenes)"; } 
      elsif ($logo=~m#xrisi#) { $party = "LAIKOS SYNDESMOS - CHRYSI AVGI (People's Association - Golden Dawn)"; } 
      elsif ($logo=~m#small#) { $party = "DHM.AR (Democratic Left)"; } 
      elsif ($logo=~m#KKE#) { $party = "K.K.E. (Communist Party of Greece)"; } 
      elsif ($logo=~m#INDEPENDENT#) { $party = "INDEPENDENT"; } 
      else { die "$logo => Unknown logo!\n"; } 

      $rec[0] = $party; 

      $rec[3]=~s#\s+# #g; # replace whitespace characters with a single space 

      # append the data in a tab delimited text file 
      open my $fh,">>:utf8","MPs.txt"; 
      print $fh join("\t",@rec)."\n"; 
      close $fh; 
     } 
    } 
} 

$sel->stop; 
+0

'do {$ name = tmpnam();用分號更好。 –

+0

非常感謝您的回答。但不幸的是這並沒有改變任何東西。 –

回答

0

你知道這些代碼是要死在裏面get_html_source呢,還是真的要死了緊接在之前或之後(例如,在調用tmpnam時,它似乎缺少一個分號)?

另一點評論是,這似乎是很多工作只是爲了刮清議員和他們的黨的名單。如果您查看頁面源代碼,則會顯示一大段base-64編碼文本,其中包含您需要的所有數據。因此,您可能會發現加載頁面更快,解碼塊並獲得所需的一切。

+0

@ KingZoingo:你可能是對的。但是對於一般的編程和特別是Perl的新手來說,我現在還不知道。你有什麼好的提示開始我的學習 - 爲此 - 最好?順便說一句:我已經添加了分號但沒有改變。不,我不知道它到底在哪裏死去。非常感謝。 –

+0

您似乎有信心在get_html_source中死去。爲了證明這一點,在之後立即添加一行以打印出某些東西,例如打印「我做到了這一點\ n」;如果你沒有看到,並且你看到第36行的印刷展示,那麼你已經證實了你的理論。除此之外,最快捷的方法是從您的PC運行網絡跟蹤,以查看請求是否正確離開您的機器並從網站獲得響應。 – AlwaysLearning

0

tmpnam函數由POSIX Perl模塊提供。它應該適用於Unix/Linux的大多數變體,但它似乎在Windows下被破壞。 我建議更換包含具有以下的使用tmpnam呼叫「有問題」的路線:

use File::Temp qw/ tempfile /; 
($fh,$name) = tempfile(); 

希望這種變化將解決這個問題,並允許腳本來完成。

這也是Perl tmpnam文檔(http://perldoc.perl.org/POSIX.html)的建議:「出於安全原因,可能在系統文檔中詳細介紹了C庫tmpnam()函數,不應使用此接口;請參閱File: :溫度」。