Perl LWP:如何打印获取的结果?

Perl LWP:如何打印获取的结果?

该代码中的类的元素是什么response:或者,我如何打印整个对象response

#!/usr/bin/env perl


# Example code from Chapter 1 of /Perl and LWP/ by Sean M. Burke

# http://www.oreilly.com/catalog/perllwp/

# [email protected]


require 5;

use strict;

use warnings;


use LWP;


my $browser = LWP::UserAgent->new();

my $response = $browser->get("http://www.oreilly.com/");

die "Couldn't access it: ", $response->status_line

 unless $response->is_success;

print $response->header("Server"), "\n";

__END__

答案1

像这样,如果你想获取HTML内容:

use strict; use warnings;

my $arg1 = "Rower";

# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;

# Create a request
my $req = HTTP::Request->new(GET => "http://pl.wikipedia.org/wiki/$arg1");

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
die $res->status_line, "\n" unless $res->is_success;

# Here we go
my $content = $res->content;
print $content;

或者如果您想直接获取$res内容对象进行检查:

use Data::Dumper;
print Dumper $res;

相关内容