ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
20.1. Fetching a URL from a Perl ScriptProblemYou have a URL that you want to fetch from a script. SolutionUse the use LWP::Simple; $content = get($URL); DiscussionThe right library makes life easier, and the LWP modules are the right ones for this task. The use LWP::Simple; unless (defined ($content = get $URL)) { die "could not get $URL\n"; } When it's run that way, however, you can't determine the cause of the error. For this and other elaborate processing, you'll have to go beyond LWP::Simple. Example 20.1 is a program that fetches a document remotely. If it fails, it prints out the error status line. Otherwise it prints out the document title and the number of bytes of content. We use three modules from LWP and one other from CPAN.
Although these aren't legitimate URLs (their format is not in the URI specification), Netscape tries to guess the URL they stand for. Because Netscape does it, most other browsers do too. The source is in Example 20.1. Example 20.1: titlebytes#!/usr/bin/perl -w # titlebytes - find the title and size of documents use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use URI::Heuristic; my $raw_url = shift or die "usage: $0 url\n"; my $url = URI::Heuristic::uf_urlstr($raw_url); $| = 1; # to flush next line printf "%s =>\n\t", $url; my $ua = LWP::UserAgent->new(); $ua->agent("Schmozilla/v9.14 Platinum"); # give it time, it'll get there my $req = HTTP::Request->new(GET => $url); $req->referer("http://wizard.yellowbrick.oz"); # perplex the log analysers my $response = $ua->request($req); if ($response->is_error()) { printf " %s\n", $response->status_line; } else { my $count; my $bytes; my $content = $response->content(); $bytes = length $content; $count = ($content =~ tr/\n/\n/); printf "%s (%d lines, %d bytes)\n", $response->title(), $count, $bytes; } When run, the program produces output like this: % titlebytes http://www.tpj.com/ http://www.tpj.com/ => The Perl Journal (109 lines, 4530 bytes) Yes, "referer" is not how "referrer" should be spelled. The standards people got it wrong when they misspelled HTTP_REFERER. Please use two r's when referring to things in English. See AlsoThe documentation for the CPAN module LWP::Simple, and the lwpcook (1) manpage that came with LWP; the documentation for the modules LWP::UserAgent, HTTP::Request, HTTP::Response, and URI::Heuristic; Recipe 20.2 |