-2

WWW-Mechanize Module を使用して http ヘッダー情報を取得するプログラム LWP::simple を使用して行った以下のコードは、WWW-Mechanize Module を使用して同じものを必要とします。

use strict; 
 use LWP::simple;
 use LWP::UserAgent; 
 use HTTP::Request;  

 my $URL = 'https://www.gmail.com/';  

 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });  
 my $header = HTTP::Request->new(GET => $URL);  
 my $request = HTTP::Request->new('GET', $URL, $header);  
 my $response = $ua->request($request);  

     print "URL:$URL\nHeaders:\n";
     print $response->headers_as_string;  

open (FILE, ">output.csv");
print FILE "URL:$URL\nHeaders:\n";  
print FILE $response->headers_as_string;
close(FILE);
4

1 に答える 1

1

あらすじは次のように述べています。

WWW::MechanizeはLWP::UserAgentの適切なサブクラスであり、 LWP:: UserAgentの任意のメソッドを使用することもできます。


use strict;
use warnings;

use WWW::Mechanize;

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

$mech->get( 'http://www.example.com' );
print $mech->dump_headers();

print " \n ==OR== \n\n";

my $header = HTTP::Request->new(GET => 'http://www.example.com');  
my $request = HTTP::Request->new('GET', 'http://www.example.com', $header);  
my $response = $mech->request($request); 
print $response->headers_as_string;

版画:

Connection: close
Date: Wed, 26 Jun 2013 03:57:01 GMT
Accept-Ranges: bytes
ETag: "780602-4f6-4db31b2978ec0"
Server: ECS (mdw/13C6)
Content-Length: 1270
Content-Type: text/html; charset=UTF-8
Last-Modified: Thu, 25 Apr 2013 16:13:23 GMT
Client-Date: Wed, 26 Jun 2013 03:57:01 GMT
Client-Peer: 93.184.216.119:80
Client-Response-Num: 1
Title: Example Domain
X-Cache: HIT
X-Meta-Charset: utf-8
X-Meta-Viewport: width=device-width, initial-scale=1

 ==OR== 

Connection: close
Date: Wed, 26 Jun 2013 03:57:01 GMT
Accept-Ranges: bytes
ETag: "780602-4f6-4db31b2978ec0"
Server: ECS (mdw/13C6)
Content-Length: 1270
Content-Type: text/html; charset=UTF-8
Last-Modified: Thu, 25 Apr 2013 16:13:23 GMT
Client-Date: Wed, 26 Jun 2013 03:57:01 GMT
Client-Peer: 93.184.216.119:80
Client-Response-Num: 1
Title: Example Domain
X-Cache: HIT
X-Meta-Charset: utf-8
X-Meta-Viewport: width=device-width, initial-scale=1
于 2013-06-26T03:58:30.980 に答える