src*_*ulo 8 url perl redirect www-mechanize lwp
所以我WWW::Mechanize用来抓取网站.它工作得很好,除非我请求一个网址,例如:
http://www.levi.com/
Run Code Online (Sandbox Code Playgroud)
我被重定向到:
http://us.levi.com/home/index.jsp
Run Code Online (Sandbox Code Playgroud)
对于我的脚本,我需要知道这个重定向发生了,我被重定向的网址是什么.无论如何使用WWW::Mechanize或检测到这个LWP然后获取重定向的URL?谢谢!
Ωme*_*ega 10
use strict;
use warnings;
use URI;
use WWW::Mechanize;
my $url = 'http://...';
my $mech = WWW::Mechanize->new(autocheck => 0);
$mech->max_redirect(0);
$mech->get($url);
my $status = $mech->status();
if (($status >= 300) && ($status < 400)) {
my $location = $mech->response()->header('Location');
if (defined $location) {
print "Redirected to $location\n";
$mech->get(URI->new_abs($location, $mech->base()));
}
}
Run Code Online (Sandbox Code Playgroud)
如果状态代码是3XX,那么您应该检查重定向URL的响应头.