问题描述
我正在使用LWP从网页下载内容,我想限制它等待页面的时间.像这样在lwp中完成:
I am using LWP to download content from web pages, and I would like to limit the amount of time it waits for a page. This is accomplished in lwp like this:
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->get($url);
这很好用,除非超时一旦达到极限,它就死了,我无法继续执行脚本!我真的很想正确处理此超时,以便可以记录该URL超时,然后继续进行下一个超时.有谁知道如何做到这一点?谢谢!
And this works fine, except for whenever the timeout reaches its limit, it just dies and I can't continue on with the script! I'd really like to handle this timeout properly so that I can record that the url had a timeout and then move on to my next one. Does anyone know how to do this? Thanks!
推荐答案
LWP :: Agent 的get()
返回一个 HTTP :: Response 对象,可用于检查错误:
LWP::Agent's get()
returns a HTTP::Response object that you can use for checking errors:
use LWP::Agent;
use HTTP::Status ();
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $response = $ua->get($url);
if ($response->is_error) {
printf "[%d] %s\n", $response->code, $response->message;
# record the timeout
if ($response->code == HTTP::Status::HTTP_REQUEST_TIMEOUT) {
...
}
}
顺便说一句,如今更好的做法是使用 Try :: Tiny 代替eval {...}
.它为您提供try {...} catch {...}
.并解决了检查if $@
的一些问题(请参见Try::Tiny
文档的背景部分).
Btw, the better practice nowadays is to use Try::Tiny instead of eval {...}
. It gives you try {...} catch {...}
. and it resolves some problems with checking if $@
(see the background section in the Try::Tiny
documentation).
这篇关于有效处理lwp超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!