WWW::Mechanize: 如何获取重定向的 URL 查询参数的值

WWW::Mechanize: How to get the value of a redirected URL's query parameter

在 Mechanize 上,可以创建一个用户代理来模拟网络浏览器

$agent = WWW::Mechanize->new();

要使用用户代理访问新网页,我执行以下操作:

$agent->get("http://some_url.com");

如果我在浏览器中输入相同的 URL,它会重定向到如下内容:

http://some_url.com?param1=value1&param2=value2

如何检索那些查询参数的值?

您的 [WWW::Mechanize][mech] 自动跟随重定向。所以当你调用你的get时,它会处于已经获取到带参数页面的状态。

因为那些是 URL 参数,我们可以看看你的 Mech 得到的 URL。有 a uri method that returns a URI object, which has a query_form method.

但首先,为了对此进行测试,我使用 Dancer2 创建了一个简单的 Web 服务器。它侦听端口 3000 并将请求重定向到 //foo?p1=bar&p2=baz.

$ perl -MDancer2 -e 'get "/" => sub { redirect "/foo?p1=bar&p2=baz" }; get "/foo" => sub { "hi" }; dance;'
>> Dancer2 v0.166001 server 10889 listening on http://0.0.0.0:3000

现在我们可以针对它进行编码了。

use strict;
use warnings;
use WWW::Mechanize;
use Data::Dumper;

my $mech = WWW::Mechanize->new;
$mech->get('http://localhost:3000');

my %params = $mech->uri->query_form;
print Dumper \%params;

在列表上下文中调用时,query_form returns key/value 对。我们可以将它们分配给哈希以获得访问它们的便捷方式。

$VAR1 = {
          'p1' => 'bar',
          'p2' => 'baz'
        };

如果您知道一个参数出现不止一次,您应该改用数组。

get WWW::Mechanize returns HTTP::Response object. On which you can run redirects method to get the complete redirection chain. For example I ran below code for google.com 的方法。

#!/usr/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
use Data::Dumper;

my $agent = WWW::Mechanize->new();

my $object = $agent->get('http://www.google.com/');

print Dumper $object->redirects;

输出:

$VAR1 = bless( {
                 '_msg' => 'Found',
                 '_request' => bless( {
                                        '_headers' => bless( {
                                                               'accept-encoding' => 'gzip',
                                                               'user-agent' => 'WWW-Mechanize/1.82'
                                                             }, 'HTTP::Headers' ),
                                        '_uri_canonical' => bless( do{\(my $o = 'http://www.google.com/')}, 'URI::http' ),
                                        '_uri' => $VAR1->{'_request'}{'_uri_canonical'},
                                        '_content' => '',
                                        '_method' => 'GET'
                                      }, 'HTTP::Request' ),
                 '_protocol' => 'HTTP/1.1',
                 '_rc' => '302',
                 '_headers' => bless( {
                                        'title' => '302 Moved',
                                        'content-length' => '261',
                                        'location' => 'http://www.google.co.in/?gfe_rd=cr&ei=3KoEWP78GYPj8weZlLXoDA',
                                        'date' => 'Mon, 17 Oct 2016 10:41:32 GMT',
                                        'accept-ranges' => 'none',
                                        'cache-control' => 'private',
                                        'client-date' => 'Mon, 17 Oct 2016 10:41:32 GMT',
                                        'connection' => 'close',
                                        'client-response-num' => 1,
                                        'content-type' => 'text/html; charset=UTF-8',
                                        '::std_case' => {
                                                          'title' => 'Title',
                                                          'set-cookie2' => 'Set-Cookie2',
                                                          'client-peer' => 'Client-Peer',
                                                          'client-date' => 'Client-Date',
                                                          'set-cookie' => 'Set-Cookie',
                                                          'base' => 'Base',

                                                          'content-base' => 'Content-Base',
                                                          'client-response-num' => 'Client-Response-Num'
                                                        }
                                      }, 'HTTP::Headers' ),
                 '_content' => '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/?gfe_rd=cr&amp;ei=3KoEWP78GYPj8weZlLXoDA">here</A>.
</BODY></HTML>   '

               }, 'HTTP::Response' );

如您所见,最终位置可以在 location header 中找到。