所以我使用WWW :: Mechanize来抓取网站.它工作得很好,除非我要求一个网址,例如:
@H_403_2@http://www.levi.com/
我被重定向到:
@H_403_2@http://us.levi.com/home/index.jsp对于我的脚本,我需要知道这个重定向发生了,我被重定向的网址是什么.无论如何使用WWW :: Mechanize或LWP检测到这个,然后获取重定向的URL?谢谢!
解决方法
@H_403_2@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()));
}
}