[英]How can I keep WWW::Mechanize from following redirects?
I have a Perl script that uses WWW::Mechanize to read from a file and perform some automated tasks on a website. 我有一个Perl脚本,它使用WWW :: Mechanize从文件中读取并在网站上执行一些自动化任务。 However, the website uses a 302 redirect after every time I request a certain page.
但是,每次请求某个页面后,网站都会使用302重定向。 I don't want to be redirected (the page that it redirects to takes too long to respond);
我不想被重定向(它重定向的页面需要很长时间才能响应); I just want to loop through the file and call the first link over and over.
我只想循环遍历文件并反复调用第一个链接。 I can't figure out how to make WWW::Mechanize NOT follow redirects.
我无法弄清楚如何使WWW :: Mechanize NOT不遵循重定向。 Any suggestions?
有什么建议么?
WWW::Mechanize
is a subclass of LWP::UserAgent
. WWW::Mechanize
是LWP::UserAgent
的子类。 So you can use any LWP::UserAgent
methods. 所以你可以使用任何
LWP::UserAgent
方法。
my $mech = WWW::Mechanize->new();
$mech->requests_redirectable([]);
WWW::Mechanize is a subclass of LWP::UserAgent; WWW :: Mechanize是LWP :: UserAgent的子类; you can set the max_redirect or requests_redirectable options in the constructor as you would with LWP::UserAgent.
您可以像使用LWP :: UserAgent一样在构造函数中设置max_redirect或requests_redirectable选项。
You can use $agent->max_redirect( 0 );, like in this example: 你可以使用$ agent-> max_redirect(0);,就像在这个例子中一样:
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
my $agent = WWW::Mechanize->new( 'autocheck' => 1, 'onerror' => undef, );
$agent->max_redirect( 0 );
$agent->get('http://www.depesz.com/test/redirect');
printf("Got HTTP/%s from %s.\n", $agent->response->code, $agent->uri);
$agent->max_redirect( 1 );
$agent->get('http://www.depesz.com/test/redirect');
printf("Got HTTP/%s from %s.\n", $agent->response->code, $agent->uri);
When running it prints: 运行时打印:
Got HTTP/302 from http://www.depesz.com/test/redirect.
Got HTTP/200 from http://www.depesz.com/.
So, with max_redirect(0) - it clearly doesn't follow redirects. 因此,使用max_redirect(0) - 它显然不遵循重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.