www :: Mechanize를 다음 리디렉션을 따라 유지하려면 어떻게해야합니까?

StackOverflow https://stackoverflow.com/questions/894672

  •  23-08-2019
  •  | 
  •  

문제

사용하는 Perl 스크립트가 있습니다 www :: 기계화 파일에서 읽고 웹 사이트에서 자동화 된 작업을 수행하려면 그러나 웹 사이트는 특정 페이지를 요청할 때마다 302 리디렉션을 사용합니다. 리디렉션되고 싶지 않습니다 (응답하는 데 너무 오래 걸리는 페이지). 파일을 반복하고 첫 번째 링크를 반복해서 호출하고 싶습니다. www :: mechanize를 반역하지 않는 방법을 알 수 없습니다. 제안이 있습니까?

도움이 되었습니까?

해결책

WWW::Mechanize 서브 클래스입니다 LWP::UserAgent. 따라서 사용할 수 있습니다 LWP::UserAgent 행동 양식.

my $mech = WWW::Mechanize->new();
$mech->requests_redirectable([]);

다른 팁

www :: Mechanize는 lwp :: useragent의 서브 클래스입니다. lwp :: useragent와 마찬가지로 생성자에서 max_redirect 또는 requests_redirectable 옵션을 설정할 수 있습니다.

이 예제에서와 같이 $ ager-> 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);

인쇄 할 때 : 인쇄 :

Got HTTP/302 from http://www.depesz.com/test/redirect.
Got HTTP/200 from http://www.depesz.com/.

따라서 Max_Redirect (0)를 사용하면 리디렉션을 분명히 따르지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top