-2

程序使用 WWW-Mechanize Module 来获取我使用 LWP::simple 完成的 http 标头信息下面是代码,我需要使用 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::MechanizeLWP::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 回答