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

这是目标站点问题。表明

503 Service Unavailable 没有可用的服务器来处理这个请求。

现在。

于 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 回答