1152 lines
32 KiB
HTML
1152 lines
32 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of lwptut</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>lwptut</H1>
|
|
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2019-11-29<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
lwptut -- An LWP Tutorial
|
|
<A NAME="lbAC"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
|
|
|
|
<FONT SIZE="-1">LWP</FONT> (short for ``Library for <FONT SIZE="-1">WWW</FONT> in Perl'') is a very popular group of
|
|
Perl modules for accessing data on the Web. Like most Perl
|
|
module-distributions, each of <FONT SIZE="-1">LWP</FONT>'s component modules comes with
|
|
documentation that is a complete reference to its interface. However,
|
|
there are so many modules in <FONT SIZE="-1">LWP</FONT> that it's hard to know where to start
|
|
looking for information on how to do even the simplest most common
|
|
things.
|
|
<P>
|
|
|
|
Really introducing you to using <FONT SIZE="-1">LWP</FONT> would require a whole book --- a book
|
|
that just happens to exist, called <I>Perl & </I><FONT SIZE="-1"><I>LWP</I></FONT><I></I>. But this article
|
|
should give you a taste of how you can go about some common tasks with
|
|
<FONT SIZE="-1">LWP.</FONT>
|
|
<A NAME="lbAD"> </A>
|
|
<H3>Getting documents with LWP::Simple</H3>
|
|
|
|
|
|
|
|
If you just want to get what's at a particular <FONT SIZE="-1">URL,</FONT> the simplest way
|
|
to do it is LWP::Simple's functions.
|
|
<P>
|
|
|
|
In a Perl program, you can call its <TT>"get($url)"</TT> function. It will try
|
|
getting that <FONT SIZE="-1">URL</FONT>'s content. If it works, then it'll return the
|
|
content; but if there's some error, it'll return undef.
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
my $url = '<A HREF="http://www.npr.org/programs/fa/?todayDate=current';">http://www.npr.org/programs/fa/?todayDate=current';</A>
|
|
# Just an example: the URL for the most recent /Fresh Air/ show
|
|
|
|
use LWP::Simple;
|
|
my $content = get $url;
|
|
die "Couldn't get $url" unless defined $content;
|
|
|
|
# Then go do things with $content, like this:
|
|
|
|
if($content =~ m/jazz/i) {
|
|
print "They're talking about jazz today on Fresh Air!\n";
|
|
}
|
|
else {
|
|
print "Fresh Air is apparently jazzless today.\n";
|
|
}
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
The handiest variant on <TT>"get"</TT> is <TT>"getprint"</TT>, which is useful in Perl
|
|
one-liners. If it can get the page whose <FONT SIZE="-1">URL</FONT> you provide, it sends it
|
|
to <FONT SIZE="-1">STDOUT</FONT>; otherwise it complains to <FONT SIZE="-1">STDERR.</FONT>
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
% perl -MLWP::Simple -e "getprint '<A HREF="http://www.cpan.org/RECENT'">http://www.cpan.org/RECENT'</A>"
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
That is the <FONT SIZE="-1">URL</FONT> of a plain text file that lists new files in <FONT SIZE="-1">CPAN</FONT> in
|
|
the past two weeks. You can easily make it part of a tidy little
|
|
shell command, like this one that mails you the list of new
|
|
<TT>"Acme::"</TT> modules:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
% perl -MLWP::Simple -e "getprint '<A HREF="http://www.cpan.org/RECENT'">http://www.cpan.org/RECENT'</A>" \
|
|
| grep "/by-module/Acme" | mail -s "New Acme modules! Joy!" $USER
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
There are other useful functions in LWP::Simple, including one function
|
|
for running a <FONT SIZE="-1">HEAD</FONT> request on a <FONT SIZE="-1">URL</FONT> (useful for checking links, or
|
|
getting the last-revised time of a <FONT SIZE="-1">URL</FONT>), and two functions for
|
|
saving/mirroring a <FONT SIZE="-1">URL</FONT> to a local file. See the LWP::Simple
|
|
documentation for the full details, or chapter 2 of <I>Perl
|
|
& </I><FONT SIZE="-1"><I>LWP</I></FONT><I></I> for more examples.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>The Basics of the <FONT SIZE="-1">LWP</FONT> Class Model</H3>
|
|
|
|
|
|
|
|
LWP::Simple's functions are handy for simple cases, but its functions
|
|
don't support cookies or authorization, don't support setting header
|
|
lines in the <FONT SIZE="-1">HTTP</FONT> request, generally don't support reading header lines
|
|
in the <FONT SIZE="-1">HTTP</FONT> response (notably the full <FONT SIZE="-1">HTTP</FONT> error message, in case of an
|
|
error). To get at all those features, you'll have to use the full <FONT SIZE="-1">LWP</FONT>
|
|
class model.
|
|
<P>
|
|
|
|
While <FONT SIZE="-1">LWP</FONT> consists of dozens of classes, the main two that you have to
|
|
understand are LWP::UserAgent and HTTP::Response. LWP::UserAgent
|
|
is a class for ``virtual browsers'' which you use for performing requests,
|
|
and HTTP::Response is a class for the responses (or error messages)
|
|
that you get back from those requests.
|
|
<P>
|
|
|
|
The basic idiom is <TT>"$response = $browser->get($url)"</TT>, or more fully
|
|
illustrated:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
# Early in your program:
|
|
|
|
use LWP 5.64; # Loads all important LWP classes, and makes
|
|
# sure your version is reasonably recent.
|
|
|
|
my $browser = LWP::UserAgent->new;
|
|
|
|
...
|
|
|
|
# Then later, whenever you need to make a get request:
|
|
my $url = '<A HREF="http://www.npr.org/programs/fa/?todayDate=current';">http://www.npr.org/programs/fa/?todayDate=current';</A>
|
|
|
|
my $response = $browser->get( $url );
|
|
die "Can't get $url -- ", $response->status_line
|
|
unless $response->is_success;
|
|
|
|
die "Hey, I was expecting HTML, not ", $response->content_type
|
|
unless $response->content_type eq 'text/html';
|
|
# or whatever content-type you're equipped to deal with
|
|
|
|
# Otherwise, process the content somehow:
|
|
|
|
if($response->decoded_content =~ m/jazz/i) {
|
|
print "They're talking about jazz today on Fresh Air!\n";
|
|
}
|
|
else {
|
|
print "Fresh Air is apparently jazzless today.\n";
|
|
}
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
There are two objects involved: <TT>$browser</TT>, which holds an object of
|
|
class LWP::UserAgent, and then the <TT>$response</TT> object, which is of
|
|
class HTTP::Response. You really need only one browser object per
|
|
program; but every time you make a request, you get back a new
|
|
HTTP::Response object, which will have some interesting attributes:
|
|
<DL COMPACT>
|
|
<DT id="1">•<DD>
|
|
A status code indicating
|
|
success or failure
|
|
(which you can test with <TT>"$response->is_success"</TT>).
|
|
<DT id="2">•<DD>
|
|
An <FONT SIZE="-1">HTTP</FONT> status
|
|
line that is hopefully informative if there's failure (which you can
|
|
see with <TT>"$response->status_line"</TT>,
|
|
returning something like ``404 Not Found'').
|
|
<DT id="3">•<DD>
|
|
A <FONT SIZE="-1">MIME</FONT> content-type like ``text/html'', ``image/gif'',
|
|
``application/xml'', etc., which you can see with
|
|
<TT>"$response->content_type"</TT>
|
|
<DT id="4">•<DD>
|
|
The actual content of the response, in <TT>"$response->decoded_content"</TT>.
|
|
If the response is <FONT SIZE="-1">HTML,</FONT> that's where the <FONT SIZE="-1">HTML</FONT> source will be; if
|
|
it's a <FONT SIZE="-1">GIF,</FONT> then <TT>"$response->decoded_content"</TT> will be the binary
|
|
<FONT SIZE="-1">GIF</FONT> data.
|
|
<DT id="5">•<DD>
|
|
And dozens of other convenient and more specific methods that are
|
|
documented in the docs for HTTP::Response, and its superclasses
|
|
HTTP::Message and HTTP::Headers.
|
|
</DL>
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Adding Other <FONT SIZE="-1">HTTP</FONT> Request Headers</H3>
|
|
|
|
|
|
|
|
The most commonly used syntax for requests is <TT>"$response =
|
|
$browser->get($url)"</TT>, but in truth, you can add extra <FONT SIZE="-1">HTTP</FONT> header
|
|
lines to the request by adding a list of key-value pairs after the <FONT SIZE="-1">URL,</FONT>
|
|
like so:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $browser->get( $url, $key1, $value1, $key2, $value2, ... );
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
For example, here's how to send some commonly used headers, in case
|
|
you're dealing with a site that would otherwise reject your request:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
my @ns_headers = (
|
|
'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
|
|
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
|
|
'Accept-Charset' => 'iso-8859-1,*,utf-8',
|
|
'Accept-Language' => 'en-US',
|
|
);
|
|
|
|
...
|
|
|
|
$response = $browser->get($url, @ns_headers);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
If you weren't reusing that array, you could just go ahead and do this:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $browser->get($url,
|
|
'User-Agent' => 'Mozilla/4.76 [en] (Win98; U)',
|
|
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*',
|
|
'Accept-Charset' => 'iso-8859-1,*,utf-8',
|
|
'Accept-Language' => 'en-US',
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
If you were only ever changing the 'User-Agent' line, you could just change
|
|
the <TT>$browser</TT> object's default line from ``libwww-perl/5.65'' (or the like)
|
|
to whatever you like, using the LWP::UserAgent <TT>"agent"</TT> method:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->agent('Mozilla/4.76 [en] (Win98; U)');
|
|
|
|
</PRE>
|
|
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H3>Enabling Cookies</H3>
|
|
|
|
|
|
|
|
A default LWP::UserAgent object acts like a browser with its cookies
|
|
support turned off. There are various ways of turning it on, by setting
|
|
its <TT>"cookie_jar"</TT> attribute. A ``cookie jar'' is an object representing
|
|
a little database of all
|
|
the <FONT SIZE="-1">HTTP</FONT> cookies that a browser knows about. It can correspond to a
|
|
file on disk or
|
|
an in-memory object that starts out empty, and whose collection of
|
|
cookies will disappear once the program is finished running.
|
|
<P>
|
|
|
|
To give a browser an in-memory empty cookie jar, you set its <TT>"cookie_jar"</TT>
|
|
attribute like so:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use HTTP::CookieJar::LWP;
|
|
$browser->cookie_jar( HTTP::CookieJar::LWP->new );
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
To save a cookie jar to disk, see ``dump_cookies'' in HTTP::CookieJar.
|
|
To load cookies from disk into a jar, see ``load_cookies'' in HTTP::CookieJar.
|
|
<A NAME="lbAH"> </A>
|
|
<H3>Posting Form Data</H3>
|
|
|
|
|
|
|
|
Many <FONT SIZE="-1">HTML</FONT> forms send data to their server using an <FONT SIZE="-1">HTTP POST</FONT> request, which
|
|
you can send with this syntax:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $browser->post( $url,
|
|
[
|
|
formkey1 => value1,
|
|
formkey2 => value2,
|
|
...
|
|
],
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
Or if you need to send <FONT SIZE="-1">HTTP</FONT> headers:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $browser->post( $url,
|
|
[
|
|
formkey1 => value1,
|
|
formkey2 => value2,
|
|
...
|
|
],
|
|
headerkey1 => value1,
|
|
headerkey2 => value2,
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
For example, the following program makes a search request to AltaVista
|
|
(by sending some form data via an <FONT SIZE="-1">HTTP POST</FONT> request), and extracts from
|
|
the <FONT SIZE="-1">HTML</FONT> the report of the number of matches:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use strict;
|
|
use warnings;
|
|
use LWP 5.64;
|
|
my $browser = LWP::UserAgent->new;
|
|
|
|
my $word = 'tarragon';
|
|
|
|
my $url = '<A HREF="http://search.yahoo.com/yhs/search';">http://search.yahoo.com/yhs/search';</A>
|
|
my $response = $browser->post( $url,
|
|
[ 'q' => $word, # the Altavista query string
|
|
'fr' => 'altavista', 'pg' => 'q', 'avkw' => 'tgz', 'kl' => 'XX',
|
|
]
|
|
);
|
|
die "$url error: ", $response->status_line
|
|
unless $response->is_success;
|
|
die "Weird content type at $url -- ", $response->content_type
|
|
unless $response->content_is_html;
|
|
|
|
if( $response->decoded_content =~ m{([0-9,]+)(?:<.*?>)? results for} ) {
|
|
# The substring will be like "996,000</strong> results for"
|
|
print "$word: $1\n";
|
|
}
|
|
else {
|
|
print "Couldn't find the match-string in the response\n";
|
|
}
|
|
|
|
</PRE>
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H3>Sending <FONT SIZE="-1">GET</FONT> Form Data</H3>
|
|
|
|
|
|
|
|
Some <FONT SIZE="-1">HTML</FONT> forms convey their form data not by sending the data
|
|
in an <FONT SIZE="-1">HTTP POST</FONT> request, but by making a normal <FONT SIZE="-1">GET</FONT> request with
|
|
the data stuck on the end of the <FONT SIZE="-1">URL.</FONT> For example, if you went to
|
|
<TT>"<A HREF="http://www.imdb.com">www.imdb.com</A>"</TT> and ran a search on ``Blade Runner'', the <FONT SIZE="-1">URL</FONT> you'd see
|
|
in your browser window would be:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
<A HREF="http://www.imdb.com/find?s=all">http://www.imdb.com/find?s=all</A>&q=Blade+Runner
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
To run the same search with <FONT SIZE="-1">LWP,</FONT> you'd use this idiom, which involves
|
|
the <FONT SIZE="-1">URI</FONT> class:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use URI;
|
|
my $url = URI->new( '<A HREF="http://www.imdb.com/find'">http://www.imdb.com/find'</A> );
|
|
# makes an object representing the URL
|
|
|
|
$url->query_form( # And here the form data pairs:
|
|
'q' => 'Blade Runner',
|
|
's' => 'all',
|
|
);
|
|
|
|
my $response = $browser->get($url);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
See chapter 5 of <I>Perl & </I><FONT SIZE="-1"><I>LWP</I></FONT><I></I> for a longer discussion of <FONT SIZE="-1">HTML</FONT> forms
|
|
and of form data, and chapters 6 through 9 for a longer discussion of
|
|
extracting data from <FONT SIZE="-1">HTML.</FONT>
|
|
<A NAME="lbAJ"> </A>
|
|
<H3>Absolutizing URLs</H3>
|
|
|
|
|
|
|
|
The <FONT SIZE="-1">URI</FONT> class that we just mentioned above provides all sorts of methods
|
|
for accessing and modifying parts of URLs (such as asking sort of <FONT SIZE="-1">URL</FONT> it
|
|
is with <TT>"$url->scheme"</TT>, and asking what host it refers to with <TT>"$url->host"</TT>, and so on, as described in the docs for the <FONT SIZE="-1">URI</FONT>
|
|
class. However, the methods of most immediate interest
|
|
are the <TT>"query_form"</TT> method seen above, and now the <TT>"new_abs"</TT> method
|
|
for taking a probably-relative <FONT SIZE="-1">URL</FONT> string (like ``../foo.html'') and getting
|
|
back an absolute <FONT SIZE="-1">URL</FONT> (like ``<A HREF="http://www.perl.com/stuff/foo.html''),">http://www.perl.com/stuff/foo.html''),</A> as
|
|
shown here:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use URI;
|
|
$abs = URI->new_abs($maybe_relative, $base);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
For example, consider this program that matches URLs in the <FONT SIZE="-1">HTML</FONT>
|
|
list of new modules in <FONT SIZE="-1">CPAN:</FONT>
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use strict;
|
|
use warnings;
|
|
use LWP;
|
|
my $browser = LWP::UserAgent->new;
|
|
|
|
my $url = '<A HREF="http://www.cpan.org/RECENT.html';">http://www.cpan.org/RECENT.html';</A>
|
|
my $response = $browser->get($url);
|
|
die "Can't get $url -- ", $response->status_line
|
|
unless $response->is_success;
|
|
|
|
my $html = $response->decoded_content;
|
|
while( $html =~ m/<A HREF=\"(.*?)\"/g ) {
|
|
print "$1\n";
|
|
}
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
When run, it emits output that starts out something like this:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
MIRRORING.FROM
|
|
RECENT
|
|
RECENT.html
|
|
authors/00whois.html
|
|
authors/01mailrc.txt.gz
|
|
authors/id/A/AA/AASSAD/CHECKSUMS
|
|
...
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
However, if you actually want to have those be absolute URLs, you
|
|
can use the <FONT SIZE="-1">URI</FONT> module's <TT>"new_abs"</TT> method, by changing the <TT>"while"</TT>
|
|
loop to this:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
while( $html =~ m/<A HREF=\"(.*?)\"/g ) {
|
|
print URI->new_abs( $1, $response->base ) ,"\n";
|
|
}
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
(The <TT>"$response->base"</TT> method from HTTP::Message
|
|
is for returning what <FONT SIZE="-1">URL</FONT>
|
|
should be used for resolving relative URLs --- it's usually just
|
|
the same as the <FONT SIZE="-1">URL</FONT> that you requested.)
|
|
<P>
|
|
|
|
That program then emits nicely absolute URLs:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
<A HREF="http://www.cpan.org/MIRRORING.FROM">http://www.cpan.org/MIRRORING.FROM</A>
|
|
<A HREF="http://www.cpan.org/RECENT">http://www.cpan.org/RECENT</A>
|
|
<A HREF="http://www.cpan.org/RECENT.html">http://www.cpan.org/RECENT.html</A>
|
|
<A HREF="http://www.cpan.org/authors/00whois.html">http://www.cpan.org/authors/00whois.html</A>
|
|
<A HREF="http://www.cpan.org/authors/01mailrc.txt.gz">http://www.cpan.org/authors/01mailrc.txt.gz</A>
|
|
<A HREF="http://www.cpan.org/authors/id/A/AA/AASSAD/CHECKSUMS">http://www.cpan.org/authors/id/A/AA/AASSAD/CHECKSUMS</A>
|
|
...
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
See chapter 4 of <I>Perl & </I><FONT SIZE="-1"><I>LWP</I></FONT><I></I> for a longer discussion of <FONT SIZE="-1">URI</FONT> objects.
|
|
<P>
|
|
|
|
Of course, using a regexp to match hrefs is a bit simplistic, and for
|
|
more robust programs, you'll probably want to use an HTML-parsing module
|
|
like HTML::LinkExtor or HTML::TokeParser or even maybe
|
|
HTML::TreeBuilder.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Other Browser Attributes</H3>
|
|
|
|
|
|
|
|
LWP::UserAgent objects have many attributes for controlling how they
|
|
work. Here are a few notable ones:
|
|
<DL COMPACT>
|
|
<DT id="6">•<DD>
|
|
<TT>"$browser->timeout(15);"</TT>
|
|
|
|
|
|
<P>
|
|
|
|
|
|
This sets this browser object to give up on requests that don't answer
|
|
within 15 seconds.
|
|
<DT id="7">•<DD>
|
|
<TT>"$browser->protocols_allowed( [ 'http', 'gopher'] );"</TT>
|
|
|
|
|
|
<P>
|
|
|
|
|
|
This sets this browser object to not speak any protocols other than <FONT SIZE="-1">HTTP</FONT>
|
|
and gopher. If it tries accessing any other kind of <FONT SIZE="-1">URL</FONT> (like an ``ftp:''
|
|
or ``mailto:'' or ``news:'' <FONT SIZE="-1">URL</FONT>), then it won't actually try connecting, but
|
|
instead will immediately return an error code 500, with a message like
|
|
``Access to 'ftp' URIs has been disabled''.
|
|
<DT id="8">•<DD>
|
|
<TT>"use LWP::ConnCache; $browser->conn_cache(LWP::ConnCache->new());"</TT>
|
|
|
|
|
|
<P>
|
|
|
|
|
|
This tells the browser object to try using the <FONT SIZE="-1">HTTP/1.1</FONT> ``Keep-Alive''
|
|
feature, which speeds up requests by reusing the same socket connection
|
|
for multiple requests to the same server.
|
|
<DT id="9">•<DD>
|
|
<TT>"$browser->agent( 'SomeName/1.23 (more info here maybe)' )"</TT>
|
|
|
|
|
|
<P>
|
|
|
|
|
|
This changes how the browser object will identify itself in
|
|
the default ``User-Agent'' line is its <FONT SIZE="-1">HTTP</FONT> requests. By default,
|
|
it'll send "libwww-perl/<I>versionnumber</I>``, like
|
|
''libwww-perl/5.65". You can change that to something more descriptive
|
|
like this:
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->agent( 'SomeName/3.14 (<A HREF="mailto:contact@robotplexus.int">contact@robotplexus.int</A>)' );
|
|
|
|
</PRE>
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
|
|
Or if need be, you can go in disguise, like this:
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->agent( 'Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC)' );
|
|
|
|
</PRE>
|
|
|
|
|
|
<DT id="10">•<DD>
|
|
<TT>"push @{ $ua->requests_redirectable }, 'POST';"</TT>
|
|
|
|
|
|
<P>
|
|
|
|
|
|
This tells this browser to obey redirection responses to <FONT SIZE="-1">POST</FONT> requests
|
|
(like most modern interactive browsers), even though the <FONT SIZE="-1">HTTP RFC</FONT> says
|
|
that should not normally be done.
|
|
</DL>
|
|
<P>
|
|
|
|
For more options and information, see the full documentation for
|
|
LWP::UserAgent.
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Writing Polite Robots</H3>
|
|
|
|
|
|
|
|
If you want to make sure that your LWP-based program respects <I>robots.txt</I>
|
|
files and doesn't make too many requests too fast, you can use the LWP::RobotUA
|
|
class instead of the LWP::UserAgent class.
|
|
<P>
|
|
|
|
LWP::RobotUA class is just like LWP::UserAgent, and you can use it like so:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use LWP::RobotUA;
|
|
my $browser = LWP::RobotUA->new('YourSuperBot/1.34', '<A HREF="mailto:you@yoursite.com">you@yoursite.com</A>');
|
|
# Your bot's name and your email address
|
|
|
|
my $response = $browser->get($url);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
But HTTP::RobotUA adds these features:
|
|
<DL COMPACT>
|
|
<DT id="11">•<DD>
|
|
If the <I>robots.txt</I> on <TT>$url</TT>'s server forbids you from accessing
|
|
<TT>$url</TT>, then the <TT>$browser</TT> object (assuming it's of class LWP::RobotUA)
|
|
won't actually request it, but instead will give you back (in <TT>$response</TT>) a 403 error
|
|
with a message ``Forbidden by robots.txt''. That is, if you have this line:
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
|
|
<PRE>
|
|
die "$url -- ", $response->status_line, "\nAborted"
|
|
unless $response->is_success;
|
|
|
|
</PRE>
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
|
|
then the program would die with an error message like this:
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
|
|
<PRE>
|
|
<A HREF="http://whatever.site.int/pith/x.html">http://whatever.site.int/pith/x.html</A> -- 403 Forbidden by robots.txt
|
|
Aborted at whateverprogram.pl line 1234
|
|
|
|
</PRE>
|
|
|
|
|
|
<DT id="12">•<DD>
|
|
If this <TT>$browser</TT> object sees that the last time it talked to
|
|
<TT>$url</TT>'s server was too recently, then it will pause (via <TT>"sleep"</TT>) to
|
|
avoid making too many requests too often. How long it will pause for, is
|
|
by default one minute --- but you can control it with the <TT>"$browser->delay( </TT>minutes<TT> )"</TT> attribute.
|
|
|
|
|
|
<P>
|
|
|
|
|
|
For example, this code:
|
|
|
|
|
|
<P>
|
|
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->delay( 7/60 );
|
|
|
|
</PRE>
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
|
|
...means that this browser will pause when it needs to avoid talking to
|
|
any given server more than once every 7 seconds.
|
|
</DL>
|
|
<P>
|
|
|
|
For more options and information, see the full documentation for
|
|
LWP::RobotUA.
|
|
<A NAME="lbAM"> </A>
|
|
<H3>Using Proxies</H3>
|
|
|
|
|
|
|
|
In some cases, you will want to (or will have to) use proxies for
|
|
accessing certain sites and/or using certain protocols. This is most
|
|
commonly the case when your <FONT SIZE="-1">LWP</FONT> program is running (or could be running)
|
|
on a machine that is behind a firewall.
|
|
<P>
|
|
|
|
To make a browser object use proxies that are defined in the usual
|
|
environment variables (<TT>"HTTP_PROXY"</TT>, etc.), just call the <TT>"env_proxy"</TT>
|
|
on a user-agent object before you go making any requests on it.
|
|
Specifically:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use LWP::UserAgent;
|
|
my $browser = LWP::UserAgent->new;
|
|
|
|
# And before you go making any requests:
|
|
$browser->env_proxy;
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
For more information on proxy parameters, see the LWP::UserAgent
|
|
documentation, specifically the <TT>"proxy"</TT>, <TT>"env_proxy"</TT>,
|
|
and <TT>"no_proxy"</TT> methods.
|
|
<A NAME="lbAN"> </A>
|
|
<H3><FONT SIZE="-1">HTTP</FONT> Authentication</H3>
|
|
|
|
|
|
|
|
Many web sites restrict access to documents by using ``<FONT SIZE="-1">HTTP</FONT>
|
|
Authentication''. This isn't just any form of ``enter your password''
|
|
restriction, but is a specific mechanism where the <FONT SIZE="-1">HTTP</FONT> server sends the
|
|
browser an <FONT SIZE="-1">HTTP</FONT> code that says ``That document is part of a protected
|
|
'realm', and you can access it only if you re-request it and add some
|
|
special authorization headers to your request''.
|
|
<P>
|
|
|
|
For example, the Unicode.org admins stop email-harvesting bots from
|
|
harvesting the contents of their mailing list archives, by protecting
|
|
them with <FONT SIZE="-1">HTTP</FONT> Authentication, and then publicly stating the username
|
|
and password (at <TT>"<A HREF="http://www.unicode.org/mail-arch/">http://www.unicode.org/mail-arch/</A>"</TT>) --- namely
|
|
username ``unicode-ml'' and password ``unicode''.
|
|
<P>
|
|
|
|
For example, consider this <FONT SIZE="-1">URL,</FONT> which is part of the protected
|
|
area of the web site:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
<A HREF="http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html">http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html</A>
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
If you access that with a browser, you'll get a prompt
|
|
like
|
|
``Enter username and password for 'Unicode-MailList-Archives' at server
|
|
'<A HREF="http://www.unicode.org">www.unicode.org</A>'''.
|
|
<P>
|
|
|
|
In <FONT SIZE="-1">LWP,</FONT> if you just request that <FONT SIZE="-1">URL,</FONT> like this:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use LWP;
|
|
my $browser = LWP::UserAgent->new;
|
|
|
|
my $url =
|
|
'<A HREF="http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html';">http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html';</A>
|
|
my $response = $browser->get($url);
|
|
|
|
die "Error: ", $response->header('WWW-Authenticate') || 'Error accessing',
|
|
# ('WWW-Authenticate' is the realm-name)
|
|
"\n ", $response->status_line, "\n at $url\n Aborting"
|
|
unless $response->is_success;
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
Then you'll get this error:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
Error: Basic realm="Unicode-MailList-Archives"
|
|
401 Authorization Required
|
|
at <A HREF="http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html">http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html</A>
|
|
Aborting at auth1.pl line 9. [or wherever]
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
...because the <TT>$browser</TT> doesn't know any the username and password
|
|
for that realm (``Unicode-MailList-Archives'') at that host
|
|
(``<A HREF="http://www.unicode.org">www.unicode.org</A>''). The simplest way to let the browser know about this
|
|
is to use the <TT>"credentials"</TT> method to let it know about a username and
|
|
password that it can try using for that realm at that host. The syntax is:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->credentials(
|
|
'servername:portnumber',
|
|
'realm-name',
|
|
'username' => 'password'
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
In most cases, the port number is 80, the default <FONT SIZE="-1">TCP/IP</FONT> port for <FONT SIZE="-1">HTTP</FONT>; and
|
|
you usually call the <TT>"credentials"</TT> method before you make any requests.
|
|
For example:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->credentials(
|
|
'reports.mybazouki.com:80',
|
|
'web_server_usage_reports',
|
|
'plinky' => 'banjo123'
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
So if we add the following to the program above, right after the <TT>"$browser = LWP::UserAgent->new;"</TT> line...
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$browser->credentials( # add this to our $browser 's "key ring"
|
|
'<A HREF="http://www.unicode.org">www.unicode.org</A>:80',
|
|
'Unicode-MailList-Archives',
|
|
'unicode-ml' => 'unicode'
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
...then when we run it, the request succeeds, instead of causing the
|
|
<TT>"die"</TT> to be called.
|
|
<A NAME="lbAO"> </A>
|
|
<H3>Accessing <FONT SIZE="-1">HTTPS</FONT> URLs</H3>
|
|
|
|
|
|
|
|
When you access an <FONT SIZE="-1">HTTPS URL,</FONT> it'll work for you just like an <FONT SIZE="-1">HTTP URL</FONT>
|
|
would --- if your <FONT SIZE="-1">LWP</FONT> installation has <FONT SIZE="-1">HTTPS</FONT> support (via an appropriate
|
|
Secure Sockets Layer library). For example:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use LWP;
|
|
my $url = '<A HREF="https://www.paypal.com/';">https://www.paypal.com/';</A> # Yes, HTTPS!
|
|
my $browser = LWP::UserAgent->new;
|
|
my $response = $browser->get($url);
|
|
die "Error at $url\n ", $response->status_line, "\n Aborting"
|
|
unless $response->is_success;
|
|
print "Whee, it worked! I got that ",
|
|
$response->content_type, " document!\n";
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
If your <FONT SIZE="-1">LWP</FONT> installation doesn't have <FONT SIZE="-1">HTTPS</FONT> support set up, then the
|
|
response will be unsuccessful, and you'll get this error message:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
Error at <A HREF="https://www.paypal.com/">https://www.paypal.com/</A>
|
|
501 Protocol scheme 'https' is not supported
|
|
Aborting at paypal.pl line 7. [or whatever program and line]
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
If your <FONT SIZE="-1">LWP</FONT> installation <I>does</I> have <FONT SIZE="-1">HTTPS</FONT> support installed, then the
|
|
response should be successful, and you should be able to consult
|
|
<TT>$response</TT> just like with any normal <FONT SIZE="-1">HTTP</FONT> response.
|
|
<P>
|
|
|
|
For information about installing <FONT SIZE="-1">HTTPS</FONT> support for your <FONT SIZE="-1">LWP</FONT>
|
|
installation, see the helpful <I></I><FONT SIZE="-1"><I>README.SSL</I></FONT><I></I> file that comes in the
|
|
libwww-perl distribution.
|
|
<A NAME="lbAP"> </A>
|
|
<H3>Getting Large Documents</H3>
|
|
|
|
|
|
|
|
When you're requesting a large (or at least potentially large) document,
|
|
a problem with the normal way of using the request methods (like <TT>"$response = $browser->get($url)"</TT>) is that the response object in
|
|
memory will have to hold the whole document --- <I>in memory</I>. If the
|
|
response is a thirty megabyte file, this is likely to be quite an
|
|
imposition on this process's memory usage.
|
|
<P>
|
|
|
|
A notable alternative is to have <FONT SIZE="-1">LWP</FONT> save the content to a file on disk,
|
|
instead of saving it up in memory. This is the syntax to use:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $ua->get($url,
|
|
':content_file' => $filespec,
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
For example,
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
$response = $ua->get('<A HREF="http://search.cpan.org/',">http://search.cpan.org/',</A>
|
|
':content_file' => '/tmp/sco.html'
|
|
);
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
When you use this <TT>":content_file"</TT> option, the <TT>$response</TT> will have
|
|
all the normal header lines, but <TT>"$response->content"</TT> will be
|
|
empty. Errors writing to the content file (for example due to
|
|
permission denied or the filesystem being full) will be reported via
|
|
the <TT>"Client-Aborted"</TT> or <TT>"X-Died"</TT> response headers, and not the
|
|
<TT>"is_success"</TT> method:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
if ($response->header('Client-Aborted') eq 'die') {
|
|
# handle error ...
|
|
|
|
</PRE>
|
|
|
|
|
|
<P>
|
|
|
|
Note that this ``:content_file'' option isn't supported under older
|
|
versions of <FONT SIZE="-1">LWP,</FONT> so you should consider adding <TT>"use LWP 5.66;"</TT> to check
|
|
the <FONT SIZE="-1">LWP</FONT> version, if you think your program might run on systems with
|
|
older versions.
|
|
<P>
|
|
|
|
If you need to be compatible with older <FONT SIZE="-1">LWP</FONT> versions, then use
|
|
this syntax, which does the same thing:
|
|
<P>
|
|
|
|
|
|
|
|
<PRE>
|
|
use HTTP::Request::Common;
|
|
$response = $ua->request( GET($url), $filespec );
|
|
|
|
</PRE>
|
|
|
|
|
|
<A NAME="lbAQ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
Remember, this article is just the most rudimentary introduction to
|
|
<FONT SIZE="-1">LWP</FONT> --- to learn more about <FONT SIZE="-1">LWP</FONT> and LWP-related tasks, you really
|
|
must read from the following:
|
|
<DL COMPACT>
|
|
<DT id="13">•<DD>
|
|
LWP::Simple --- simple functions for getting/heading/mirroring URLs
|
|
<DT id="14">•<DD>
|
|
<FONT SIZE="-1">LWP</FONT> --- overview of the libwww-perl modules
|
|
<DT id="15">•<DD>
|
|
LWP::UserAgent --- the class for objects that represent ``virtual browsers''
|
|
<DT id="16">•<DD>
|
|
HTTP::Response --- the class for objects that represent the response to
|
|
a <FONT SIZE="-1">LWP</FONT> response, as in <TT>"$response = $browser->get(...)"</TT>
|
|
<DT id="17">•<DD>
|
|
HTTP::Message and HTTP::Headers --- classes that provide more methods
|
|
to HTTP::Response.
|
|
<DT id="18">•<DD>
|
|
<FONT SIZE="-1">URI</FONT> --- class for objects that represent absolute or relative URLs
|
|
<DT id="19">•<DD>
|
|
URI::Escape --- functions for URL-escaping and URL-unescaping strings
|
|
(like turning ``this & that'' to and from ``this%20%26%20that'').
|
|
<DT id="20">•<DD>
|
|
HTML::Entities --- functions for HTML-escaping and HTML-unescaping strings
|
|
(like turning ``C. & E. Brontë'' to and from ``C. &amp; E. Bront&euml;'')
|
|
<DT id="21">•<DD>
|
|
HTML::TokeParser and HTML::TreeBuilder --- classes for parsing <FONT SIZE="-1">HTML</FONT>
|
|
<DT id="22">•<DD>
|
|
HTML::LinkExtor --- class for finding links in <FONT SIZE="-1">HTML</FONT> documents
|
|
<DT id="23">•<DD>
|
|
The book <I>Perl & </I><FONT SIZE="-1"><I>LWP</I></FONT><I></I> by Sean M. Burke. O'Reilly & Associates,
|
|
2002. <FONT SIZE="-1">ISBN: 0-596-00178-9,</FONT> <<A HREF="http://oreilly.com/catalog/perllwp/">http://oreilly.com/catalog/perllwp/</A>>. The
|
|
whole book is also available free online:
|
|
<<A HREF="http://lwp.interglacial.com">http://lwp.interglacial.com</A>>.
|
|
</DL>
|
|
<A NAME="lbAR"> </A>
|
|
<H2>COPYRIGHT</H2>
|
|
|
|
|
|
|
|
Copyright 2002, Sean M. Burke. You can redistribute this document and/or
|
|
modify it, but only under the same terms as Perl itself.
|
|
<A NAME="lbAS"> </A>
|
|
<H2>AUTHOR</H2>
|
|
|
|
|
|
|
|
Sean M. Burke <TT>"<A HREF="mailto:sburke@cpan.org">sburke@cpan.org</A>"</TT>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="24"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="25"><A HREF="#lbAC">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="26"><A HREF="#lbAD">Getting documents with LWP::Simple</A><DD>
|
|
<DT id="27"><A HREF="#lbAE">The Basics of the <FONT SIZE="-1">LWP</FONT> Class Model</A><DD>
|
|
<DT id="28"><A HREF="#lbAF">Adding Other <FONT SIZE="-1">HTTP</FONT> Request Headers</A><DD>
|
|
<DT id="29"><A HREF="#lbAG">Enabling Cookies</A><DD>
|
|
<DT id="30"><A HREF="#lbAH">Posting Form Data</A><DD>
|
|
<DT id="31"><A HREF="#lbAI">Sending <FONT SIZE="-1">GET</FONT> Form Data</A><DD>
|
|
<DT id="32"><A HREF="#lbAJ">Absolutizing URLs</A><DD>
|
|
<DT id="33"><A HREF="#lbAK">Other Browser Attributes</A><DD>
|
|
<DT id="34"><A HREF="#lbAL">Writing Polite Robots</A><DD>
|
|
<DT id="35"><A HREF="#lbAM">Using Proxies</A><DD>
|
|
<DT id="36"><A HREF="#lbAN"><FONT SIZE="-1">HTTP</FONT> Authentication</A><DD>
|
|
<DT id="37"><A HREF="#lbAO">Accessing <FONT SIZE="-1">HTTPS</FONT> URLs</A><DD>
|
|
<DT id="38"><A HREF="#lbAP">Getting Large Documents</A><DD>
|
|
</DL>
|
|
<DT id="39"><A HREF="#lbAQ">SEE ALSO</A><DD>
|
|
<DT id="40"><A HREF="#lbAR">COPYRIGHT</A><DD>
|
|
<DT id="41"><A HREF="#lbAS">AUTHOR</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:47 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|