Perl模块程序,用于处理来自Http标头的数据,并以.csv格式向外部阅读器显示一些良好的可读性和有意义的数据

时间:2013-06-27 06:21:18

标签: perl automation perl-module

我有一个Perl模块来处理来自Http标头的数据,并以csv格式向外部阅读器显示一些良好的可读性和有意义的数据。下面是显示http标头的代码。 任何人都可以帮助我获取有关Http标头所需信息的代码并以csv格式显示它。

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

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;

1 个答案:

答案 0 :(得分:1)

它还不清楚你想要什么。好的,你想要CSV格式的所有标题,对吧?但是哪种格式?只是用逗号分隔标题? ......也许你想要这个:

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

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

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 );
open( my $fh, '>', $filename ) or die "Cant open $filename for writing";

foreach my $header ( split /\v/, $response->headers()->as_string ) {
  print $fh $header;
  print $fh ";";
}
print $fh "\n";
close $fh;