0

WWW-Mechanize を使用して Perl スクリプトを作成しようとしています。これが私のコードです:

use DBI;
use JSON;
use WWW::Mechanize;

sub fetch_companies_list
{
    my $url = shift;
    my $browser = WWW::Mechanize->new( stack_depth => 0 );
    my ($content, $json, $parsed_text, $company_name, $company_url);
    eval
    {
        print "Getting the companies list...\n";
        $browser->get( $url );
#       die "Can't get the companies list.\n" unless( $browser->status );
        $content = $browser->content();
#       die "Can't get companies names.\n" unless( $browser->status );
        $json = new JSON;
        $parsed_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode( $content );
        foreach(@$parsed_text)
        {
            $company_name = $_->{name};
            fetch_company_info( $company_name, $browser );
        }
    }
}

fetch_companies_list( "http://api.crunchbase.com/v/1/companies.js" );

問題は次のとおりです。

  1. スクリプトを開始すると、問題なく終了します。
  2. スクリプトを再開します。スクリプトは「$browser->get()」で失敗します。

しばらく待つ必要があります(約5分)。その後、再び機能し始めます。

私は Linux で作業しており、WWW-Mechanize バージョン 1.66 を使用しています。

何が問題なのですか?コンピューターにもルーターにもファイアウォールがインストールされていません。さらに、「die ...」行のコメントを外しても、get() 呼び出し内で停止するため、役に立ちません。最新の 1.71 にアップグレードすることはできますが、他の誰かがこの Perl モジュールでこれを経験したかどうか知りたいです。

4

3 に答える 3

2

5分(300秒)がデフォルトのタイムアウトです。正確にタイムアウトしたものが応答のステータス行に返されます。

my $response = $mech->res;
if (!$response->is_success()) {
   die($response->status_line());
}
于 2011-12-05T06:29:35.317 に答える
0

これはターゲットサイトの問題です。それが示している

503ServiceUnavailableこのリクエストを処理するために利用できるサーバーはありません。

たった今。

于 2011-12-05T06:18:39.123 に答える
0

待機して再試行、これを試してください

## set maximum no of tries
my $retries = 10;
## number of secs to sleep
my $sleep = 1;
do {
    eval {
        print "Getting the companies list...\n";
        $browser->get($url);

        #       die "Can't get the companies list.\n" unless( $browser->status );
        $content = $browser->content();

        #       die "Can't get companies names.\n" unless( $browser->status );
        $json        = new JSON;
        $parsed_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
        foreach (@$parsed_text) {
            $company_name = $_->{name};
            fetch_company_info( $company_name, $browser );
        }
    };

    if ($@) {
        warn $@;
        ## rest for some time
        sleep($sleep);
        ## increase the value of $sleep exponetially
        $sleep *= 2;
    }
} while ( $@ && $retries-- );
于 2011-12-09T11:34:17.127 に答える