所以我正在使用WWW::Mechanize
抓取网站。它工作得很好,除非我请求一个 url,例如:
http://www.levi.com/
我被重定向到:
http://us.levi.com/home/index.jsp
对于我的脚本,我需要知道发生了此重定向以及我被重定向到的 url 是什么。有没有办法检测这个WWW::Mechanize
or LWP
然后获取重定向的url?谢谢!
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()));
}
}
如果状态码是3XX,那么您应该检查响应标头中的重定向 url。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)