Skip to content

Latest commit

 

History

History
executable file
·
1566 lines (1031 loc) · 42.8 KB

File metadata and controls

executable file
·
1566 lines (1031 loc) · 42.8 KB

NAME

lwptut -- LWP ��勉����ャ�若����ŝ�≪�

DESCRIPTION

LWP ("Library for WWW in Perl" ��勛�㊦軒綵�) ��壔��Web 筝���勉����若�帥� ��≪�壔�祉�鴻�������������勰��絽吾����������� Perl ��≪�吾�ャ�若�̥召��с����� ��祉�������í�� Perl ��≪�吾�ャ�若�̹��絽�������罕����LWP ��勉�潟�潟����若����潟����≪�吾�ャ�若�˨� ��������������˨�壠�������ŝ�ゃ�潟�帥�若����с�若�鴻�勉�ŝ����<�㋘�潟�号����吾�����罌宴�������⓾����障����� ������������LWP ��˨�壔�������������勉�≪�吾�ャ�若�˨����������勉�с�����������膣���ф�����筝���㋜���� �����������������������勖����宴�с����������í���������「�����違�������勉����������������˨�������с�����

LWP ��勌戎�����鴻��茯㋛����������˨�壩�� 1 ������綽�荀���с�� -- �����障����� Perl & LWP ��������� ��㋘����������障����� �����������������勤��篋���� LWP ��с�������ゃ����勌����㋜����ŭ��罐㏍����������号����勉����������� 茯㋛�������障�����

Getting documents with LWP::Simple

(LWP::Simple ���篏帥�c�〠����吾�����緇�������)

�����̥�劫����� URL ��勐��絎鴻�����緇������������ŝ�����������膂≦����ʋ�号���� LWP::Simple ��勰�∽�違��篏帥����������с�����

Perl �����㏍�違�í��筝㏍�с�������勉�≪�吾�ャ�若�˨�� get($url) ��∽�違����若�喝�冴����障����� ��������壩��絎���������� URL ��勐��絎鴻�����緇������������������障����� �����障�頫������違�����絎鴻��菴������障��; ������������������篏���������í�若��莎激�������違�� ��ɱ��臂ï�ゃ��菴������障�����

my $url = 'http://freshair.npr.org/dayFA.cfm?todayDate=current';
  # 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";
}

������箴水�í�� get ��勉����ŝ����若�激�с�潟�� getprint ��с��Perl 1茵��������� ��������с����� ���絎������� URL �����������若�吾�����緇���с�������違�����絎鴻�� STDOUT �����阪�������障��; ��������ŝ�������� STDERR ��˨����í�若����阪�������障�����

% perl -MLWP::Simple -e "getprint 'http://cpan.org/RECENT'"

��������� CPAN �����勰���サ 2 ��演����勖�域�������<�ゃ�˨�勌��荀с�勉����㋘�若�潟����㏍�鴻�������<�ゃ�˨� URL ��с����� ��������˨����c�⓾�<����c����������激�с�˨�潟����潟����勌�������������☖戎�����障��; 箴������� ��違����� Acme:: ��≪�吾�ャ�若�˨�勌��荀с����<�若�˨�������˨��:

% perl -MLWP::Simple -e "getprint 'http://cpan.org/RECENT'"  \
   | grep "/by-module/Acme" | mail -s "New Acme modules! Joy!" $USER

LWP::Simple ��˨�壔����勌����˨��箴水�í�ʒ�∽�違����������障��; URL ��� HEAD ��ŝ�壔����鴻����� ��������∽�� (��ŝ�潟�壔�勉����с����壔����������� URL ��勖��腟���贋�井�ユ����勐��緇���˩梢��í�с��) ������ URL ��勐��絎鴻����㏍�若�˨�˨����<�ゃ�˨�˩��絖�/�����í�若�������������勌����ゃ�勰�∽�違�ŝ�í�с����� 絎������ʃ¼膣違�˨�ゃ����⓾�� LWP::Simple ��勖����� ��� ��眼�ŝ��箴���˨�ゃ����⓾�� Perl & LWP ��勛⓬ 2 腴���������с����⓾��������������

The Basics of the LWP Class Model

(LWP ��壔�í�鴻�≪����˨�勐�堺��)

LWP::Simple ��勰�∽�違�壠��膣���ɾ�倶����с�壚梢��í�с�������������勰�∽�違�壔�壔����㏍�若��茯�荐若� 絲上�������⓾����障�����������HTTP ��ŝ�壔����鴻����勉��������茵���勤┃絎���˨��絲上�������障����������� 筝���㋜����˨�� HTTP ��㋘�鴻����潟�鴻�勉��������茵���勤˛��粋昭���(��鴻�˨�������í�惹���� 絎������� HTTP �����í�若�<����祉�若��)���絲上�������⓾����障�������� �����������勖����遵����⓾��篏帥����˨�壔��絎������� LWP ��壔�í�鴻�≪����˨��篏帥��綽�荀������������障�����

LWP ��壩�医����勉�壔�í�鴻�ф�������������⓾����障�����������茹c�����綽�荀����������筝祉�ŭ����ゃ�勉����勉� LWP::UserAgent ��� HTTP::Response ��с����� LWP::UserAgent ��壔�ŝ�壔����鴻�����絎�茵���������������˩戎������篁勖�潟����í�⓾�吟����с�� HTTP::Response ��壔����勉�ŝ�壔����鴻��������菴������������㋘�鴻����潟��(����������� �����í�若�<����祉�若��) ��勉�������勉�壔�í�鴻�с�����

��堺�㋜����ʋ�g��羈���� $response = $browser->get($url) ��с��������絨���� 絎������̥ず������:

# 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 = 'http://freshair.npr.org/dayFA.cfm?todayDate=current';

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";
}

篋���ゃ�勉�ŝ����吾�с�壔�������≪����c�⓾����障��: $browser ��� LWP::UserAgent ��壔�í�鴻� ��ŝ����吾�с�壔�����篆����������$response ��ŝ����吾�с�壔����� HTTP::Response ��壔�í�鴻�с����� ��㋚�����綽�荀���ŝ����í�⓾�吟�ŝ����吾�с�壔����� 1 �����㏍�違�í��筝㏍�˩����ゃ�������с��; �����������ŝ�壔����鴻�������冴��罸������違����� HTTP::Response ��ŝ����吾�с�壔�����菴���������� ��������˨�壔�������ゃ����勤����恰訓���絮���с��篆���������⓾����障��:

  • ���������紊掩��������腓冴����⓾�������鴻����若�帥�鴻�潟�若��($response->is_success �� �����鴻����с����障��)���

  • 紊掩����������������勖����宴�˨�ŝ����������������ŝ�� HTTP ��鴻����若�帥�壕�� ($response->status_line ��ц����������������с�����"404 Not Found" �� ��������ŝ����勉��菴���������障��)���

  • "text/html", "image/gif", "application/xml" ��勉�������� MIME ��潟�潟����潟����帥�ゃ��; $response->content_type ��ц����������������с����障�����

  • $response->decoded_content ��˨�������㋘�鴻����潟�鴻�勐�������勐��絎鴻�� ��㋘�鴻����潟�鴻�� HTML ��勐�翫�������������� HTML ��純�若�鴻����ャ�c�⓾�������贋����с��; GIF ��勐�翫�����$response->decoded_content ��� GIF �����若�帥����ゃ����ŝ�с�����

  • ��������⓾�������������勉����勌����勌梢��í�с�������号����勉�<�純�������壔�� HTML::Response ��������潟����勉�鴻�若����若�壔�í�鴻�с����� HTML::Message ��� HTML::Headers ��勖����吾�ф����後����������⓾����障�����

Adding Other HTTP Request Headers

(�����勌����� HTTP ��ŝ�壔����鴻��������������菴遵��������)

��ŝ�壔����鴻����勉�������勖�����筝���㋜����ŭ戎�����鴻�勖��羈��� $response = $browser->get($url) ��с��������絎������勖�����篁ヤ����勉�������˨�� URL ��勐����˨�㏍��/��ゃ�勛����勉�ŝ�鴻�����菴遵����������������ц申������ HTTP ������������ 菴遵����с����障��:

$response = $browser->get( $url, $key1, $value1, $key2, $value2, ... );

箴������違��Netscape 蘂���勉����������ŝ����с�壔�ŝ�壔����鴻����������⓾�������泣�ゃ�������宴���������� �����勉�������ŝ�����������菴遵����������˨��:

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);

��������������ñ�������ŝ����ŝ����������˩札筝���勉�������˨�с����障��:

$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',
);

������ 'User-Agent' 茵����������紊���眼�������ŝ�����LWP::UserAgent ��� agent ��<�純�������� 篏帥�c�⓾��$browser ��ŝ����吾�с�壔����勉�������í�˨��茵���с����� "libwww-perl/5.65" (�����������壚室�����������ŝ�����) ������絅純�帥�勉����勉��紊���眼�с����障��:

$browser->agent('Mozilla/4.76 [en] (Win98; U)');

Enabling Cookies

(��壔����㏍�若�������鴻�˨�����)

��������í�˨����� LWP::UserAgent ��ŝ����吾�с�壔����壔����壔����㏍�弱嚳綽������ŝ����˨����� �����í�⓾�吟�勉����������壔����������障����� cookie_jar 絮���с��荐㊤����������������ф����鴻�˨�������������ゃ����勖�号�������������障����� �����壔����㏍�弱俉������("cookie jar") ��壔�������í�⓾�吟����ャ�c�⓾����������⓾�� HTTP ��壔����㏍�若�勉����若�帥����若�鴻��茵◐憗�������ŝ����吾�с�壔����с����� ��������壔����c�鴻�壚����勉����<�ゃ�� (Netscape ��� cookies.txt �����<�ゃ�˨� 篏帥�c�⓾�������号��)�����������̥Ã���������紮������⓾����㏍�違�í��腟�篋�������羔������⓾����障�� ��<�≪�ŭ����勉�ŝ����吾�с�壔�����絲上����������������������с����障�����

��<�≪�ŭ����̥Ã��勉�壔����㏍�弱俉�����������í�⓾�吟�̬┃絎���������˨�壔��篁ヤ����勉������� cookie_jar 絮���с�̬┃絎������障��:

$browser->cookie_jar({});

�����c�鴻�壚����勉����<�ゃ�˨�����茯㏍�粋昭�����������若�帥�����絎������⓾�������㏍�違�í��腟�篋������ 篆�絖���������������˨�壔��cookie_jar 絮���с��篁ヤ����勉�������̬┃絎������障��:

use HTTP::Cookies;
$browser->cookie_jar( HTTP::Cookies->new(
  'file' => '/some/where/cookies.lwp',
      # where to read/write cookies
  'autosave' => 1,
      # save it to disk when done
));

�����勉����<�ゃ�˨�� LWP ��堺����勐就綣���с����� Netscape ��勉�壔����㏍�若����<�ゃ�˨�勉�壔����㏍�若����≪�壔�祉�鴻�������������˨�������˨�壔�� HTTP::Cookies::Netscape ��壔�í�鴻��篏帥����障��:

use HTTP::Cookies;
  # yes, loads HTTP::Cookies::Netscape too

$browser->cookie_jar( HTTP::Cookies::Netscape->new(
  'file' => 'c:/Program Files/Netscape/Users/DIR-NAME-HERE/cookies.txt',
      # where to read cookies
));

筝�菴違�勉�������� 'autosave' => 1 茵����菴遵�������������������с����障�������� ��吾��莨若�炊����� Netscape ��������c�鴻�壔����吾����祉����������������壔����㏍�若�勌�������� ��贋�������������í�������壚��腆阪����с�����

Posting Form Data

(�����í�若�������若�帥�����腮帥�����)

紊������� HTML �����í�若����� HTTP POST ��ŝ�壔����鴻�����篏帥�c�⓾�泣�若����˨����若�帥�� ��������障����������������˨�壚札筝���勉�������ʋ��羈����篏帥����障��:

$response = $browser->post( $url,
  [
    formkey1 => value1, 
    formkey2 => value2, 
    ...
  ],
);

������������ HTTP ������������������綽�荀������������翫�����:

$response = $browser->post( $url,
  [
    formkey1 => value1, 
    formkey2 => value2, 
    ...
  ],
  headerkey1 => value1, 
  headerkey2 => value2, 
);

箴������違��篁ヤ����勉����㏍�違�í����� AltaVista ��� (�����í�若�������若�帥�� HTTP POST ��ŝ�壔����鴻��腟���宴�ч��篆<��������������) 罎�膣≪�ŝ�壔����鴻����������c�⓾��HTML ������ �����������潟�違�勖�違�勐�怨�����絮���������障��:

use strict;
use warnings;
use LWP 5.64;
my $browser = LWP::UserAgent->new;

my $word = 'tarragon';

my $url = 'http://www.altavista.com/sites/search/web';
my $response = $browser->post( $url,
  [ 'q' => $word,  # the Altavista query string
    '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_type eq 'text/html';

if( $response->decoded_content =~ m{AltaVista found ([0-9,]+) results} ) {
  # The substring will be like "AltaVista found 2,345 results"
  print "$word: $1\n";
}
else {
  print "Couldn't find the match-string in the response\n";
}

Sending GET Form Data

(GET �����í�若�������若�帥�����篆<�����)

HTML �����í�若����˨�壔�������í�若�������若�帥����������勉�� HTTP POST ��ŝ�壔����鴻���� ��������勉�с�壔�ŝ�����URL ��勖��絨障�˨����若�帥��篁������� ���絽吾�� GET ��ŝ�壔����鴻���� �����������勉����������障����� 箴������違��imdb.com ��� "Blade Runner" ���罎�膣≪����������������í�⓾�吟�⓾�c�潟����⓾� 茵◐ず��������� URL ���:

http://us.imdb.com/Tsearch?title=Blade%20Runner&restrict=Movies+and+TV

������罎�膣≪�� LWP ��у��茵���������˨�壔��URI ���篏帥�c�������勐�����������篏帥����障��:

use URI;
my $url = URI->new( 'http://us.imdb.com/Tsearch' );
  # makes an object representing the URL

$url->query_form(  # And here the form data pairs:
  'title'    => 'Blade Runner',
  'restrict' => 'Movies and TV',
);

my $response = $browser->get($url);

HTML �����í�若����������í�若�������若�帥�̹�≪�������������激��茘域����˨�ゃ����⓾��Perl & LWP �� 膃� 5 腴�������HTML ��������勉����若�帥�勖�遵�冴�̹�≪�������������激��茘域����˨�ゃ����⓾� 膃� 6 腴�������膃� 9 腴���������с����⓾��������������

Absolutizing URLs

(URL ��勛偽絲上��)

�����̬Е������ URI ��壔�í�鴻�壔��URL ��勌�������˨�≪�壔�祉�鴻��������篆勖③��������������� ������������腮勰����勉�<�純�����������箴������⓾����障�� ($url->scheme ��� URL ��勛┏蕁����茯帥�鴻��������$url->host �� �����с����⓾����������鴻�����茯帥�鴻����������ŝ�í�с��; ������������ URI ��壔�í�鴻�勖����� �� 荐�菴違�������⓾����障��)��� �����������������c�������贋・���荀���ŝ�<�純�������壚��菴違�� query_form ������ 篁ヤ����勉�������� ("../foo.html" ��勉��������) ��������������後嚳��� URL ��������c� ("http://www.perl.com/stuff/foo.html" ��勉��������)腟九嚳 URL ���菴���� new_abs ��<�純�������с��:

use URI;
$abs = URI->new_abs($maybe_relative, $base);

箴������違��CPAN ��勖�違�������≪�吾�ャ�若�˨�� HTML ��ŝ�鴻����˨����� URL ��˨����������潟�違����� �����勉����㏍�違�í�������������障��:

use strict;
use warnings;
use LWP;
my $browser = LWP::UserAgent->new;

my $url = 'http://www.cpan.org/RECENT.html';
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";
}

絎�茵������������������勉�������ŝ����勉����阪�������障��:

MIRRORING.FROM
RECENT
RECENT.html
authors/00whois.html
authors/01mailrc.txt.gz
authors/id/A/AA/AASSAD/CHECKSUMS
...

������������絎������˨�夌偽絲� URL �����祉�������翫�����URI ��≪�吾�ャ�若�˨� new_abs ��<�純��������篏帥�c�⓾��while ��˨�若����������勉��������紊���眼�с����障��:

while( $html =~ m/<A HREF=\"(.*?)\"/g ) {
  print URI->new_abs( $1, $response->base ) ,"\n";
}

(HTTP::Message ��� $response->base ��<�純�������壔�� ��後嚳 URL ���茹f浦��������������˨�í�� URL ���篏帥��������������菴���������障�� -- ��������壩�勰�� 荀�羆������� URL �����������с�����)

�����勉����㏍�違�í����壔�������������̥偽絲� URL �����阪�������障��:

http://www.cpan.org/MIRRORING.FROM
http://www.cpan.org/RECENT
http://www.cpan.org/RECENT.html
http://www.cpan.org/authors/00whois.html
http://www.cpan.org/authors/01mailrc.txt.gz
http://www.cpan.org/authors/id/A/AA/AASSAD/CHECKSUMS
...

URI ��ŝ����吾�с�壔����̹�≪�������������激��茯㋛����˨�ゃ����⓾�� Perl & LWP ��勛⓬ 4 腴���� �����с����⓾��������������

�����<��������href ��勉����������潟�違��罩h�頫◐憗��篏帥����勉�壠��������膣���������������с��; �����������≪�ŝ����㏍�違�í����勉�������˨�壔��HTML::LinkExtor, HTML::TokeParser, HTML::TreeBuilder ��勉�������� HTML �����若�鴻�≪�吾�ャ�若�˨��篏帥����������с�����������

Other Browser Attributes

(�����勌����勉����í�⓾�九�����)

LWP::UserAgent ��ŝ����吾�с�壔����˨�壔�����篏������九勝��������������勐�������勐����с����������障����� 羈◐�勉�������鴻�������勉����������ゃ��腓冴����障��:

  • $browser->timeout(15);

    ��������壔����ŝ�壔����鴻����� 15 腱�篁ュ����̬��篋������ŝ����◑ɗ�������������� �����í�⓾�吟�ŝ����吾�с�壔�����荐㊤�������障�����

  • $browser->protocols_allowed( [ 'http', 'gopher'] );

    ��������壔��HTTP ��� gopher 篁ュ����勉����㏍����潟�˨��荅宴����ŝ����������˨�ŝ����吾�с�壔���� 荐㊤�������障����� ("ftp:" ��� "mailto:" ��� "news:" URL ��勉��������)�����勌����勛┏蕁���� URL �� ��≪�壔�祉�鴻�����������������������絎��������・膓���������������壔����������眼�<�˨����í�若�潟�若�� 500 �� "Access to 'ftp' URIs has been disabled" ��勉�������ŝ�<����祉�若�吾�ц�������障�����

  • use LWP::ConnCache; $browser->conn_cache(LWP::ConnCache->new());

    ��������壔����������泣�若����吾�勤����違�勉�ŝ�壔����鴻�����絲障����☗�������純�宴������・膓���� �����ñ����������������с�ŝ�壔����鴻�����蕭����������������HTTP/1.1 "Keep-Alive" 罘���純�� 篏帥����������˨����í�⓾�吟�ŝ����吾�с�壔����̬┃絎������障�����

  • $browser->agent( 'SomeName/1.23 (more info here maybe)' )

    ��������壔����í�⓾�吟�ŝ����吾�с�壔����� HTTP ��ŝ�壔����鴻����勉�������í�˨���� "User-Agent" 茵���т��������箙����������紊���眼����障����� ��������í�˨����с�壔��"libwww-perl/5.65" ��勉�������˨�� "libwww-perl/�����若�吾�с�括�ɱ��" �����������障����� 篁ヤ����勉�������ŝ����c�◑ą��������ŝ����勉��紊���眼�с����障��:

    $browser->agent( 'SomeName/3.14 (contact@robotplexus.int)' );

    �����������壔��������綽�荀���ŝ�����篁ヤ����勉����������処�������������������堺�ャ�障��:

    $browser->agent( 'Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC)' );
  • push @{ $ua->requests_redirectable }, 'POST';

    ��������壔�������勉����í�⓾�吟�� POST ��ŝ�壔����鴻�����絲障�������ŝ����ゃ�㋘�壔��荀�羆���� (���菴���勉�祉�������í�勐嚳荅援�������í�⓾�吟�����罕�) �����⓾��������������腓冴����障�� (��������� HTTP ��� RFC ��壩�勰����壩����⓾�������鴻����с�壔�ŝ����������⓾����障��);

��������ŝ����ŝ����激�с�潟�������宴�˨�ゃ����⓾�壔��LWP::UserAgent ��勐������� ������ ��������с����⓾��������������

Writing Polite Robots

(腓弱��罩c�������������������吾�

�����ŝ����� LWP �����若�鴻�勉����㏍�違�í����� robots.txt ���絨���������⓾��筝�綺⓾��紊������ ��ŝ�壔����鴻�������������ŝ����������˨�������˨�壔��LWP::UserAgent ��壔�í�鴻�勌撮�������� LWP::RobotUA ��壔�í�鴻��篏帥����障�����

LWP::RobotUA ��壔�í�鴻�� LWP::UserAgent��勉�������ŝ����勉�с��篁ヤ����勉�������˨���� 篏帥����障��:

use LWP::RobotUA;
my $browser = LWP::RobotUA->new('YourSuperBot/1.34', 'you@yoursite.com');
  # Your bot's name and your email address

my $response = $browser->get($url);

������������HTTP::RobotUA ��˨�壚札筝���勉�������ʋ����純��菴遵����������⓾����障��:

  • $url ��勉�泣�若����� robots.txt ��� $url ��吾�勉�≪�壔�祉�鴻��胼�罩≪����⓾�������ŝ����� $browser ��ŝ����吾�с�壔�� (LWP::RobotUA ��壔�í�鴻��篁勐�������⓾����障��) ��壠������� ��ŝ�壔����鴻�������������������壔��������篁c�������� ($response ���) "Forbidden by robots.txt" �����������<����祉�若�娯�������� 403 �����í�若��菴������障����� ��ゃ�障�����篁ヤ����勉�������˨��������:

    die "$url -- ", $response->status_line, "\nAborted"
     unless $response->is_success;

    �����勉����㏍�違�í����壚札筝���勉�������ŝ����í�若�<����祉�若�吾����阪�������� die �����障��:

    http://whatever.site.int/pith/x.html -- 403 Forbidden by robots.txt
    Aborted at whateverprogram.pl line 1234
  • ������ $browser ��ŝ����吾�с�壔�����������緇���� $url ��勉�泣�若����◒��篆<�������勉�� ���菴������������翫����������障����紫����˨�ŝ�壔����鴻�������������������ŝ����������˨��(sleep ��� 篏帥�c��) 筝�������罩≪����障����� 筝�������罩≪�������������壔����������í�˨����с�� 1 �����с�� -- ��������� $browser->delay( minutes ) 絮���с�у�九勝��с����障�����

    箴������違�������勉�潟�若�����:

    $browser->delay( 7/60 );

    �����勉����í�⓾�吟�壔����í�勉�泣�若�����絲障����⓾�� 7 腱���� 1 ���篁ヤ���・膓������ŝ���������� ��������������˩��������罩≪�������������������潟����障�����

��������ŝ����ŝ����激�с�潟�������宴�˨�ゃ����⓾�壔��LWP::RobotUA ��勐������� ������ ��������с����⓾��������������

Using Proxies

(�����㏍�㏍�激��篏帥��)

��翫����˨����c�⓾�壔��������腮勉�勉�泣�ゃ����吾�勉�≪�壔�祉�鴻��������腮勉�勉����㏍����潟�˨�勌戎�����勉������� �����㏍�㏍�激��篏睡�����������(�����������壚戎���������綽�荀����������)�����������������障����� ��������壔��LWP ���篏帥�c�������㏍�違�í����������<�ゃ�≪�⓾�í�若�˨�勤��緇���勉����激�潟�у��茵���������� (�����������壠��茵������������������������ŝ��)�����˨����c�����筝���㋜����ɾ�倶����с�����

�����í�⓾�吟�ŝ����吾�с�壔��������絽吾�勛�医��紊����(HTTP_PROXY ��ŝ��)��у��臂í����⓾����� �����㏍�㏍�激��篏帥����������˨�������˨�壔�������˨�ŝ�壔����鴻����勐����˨�⓾�若�吟�若����若�吾�с�潟�� ��ŝ����吾�с�壔����� env_proxy �����若�喝�冴����������с����� ��隙�������˨��:

use LWP::UserAgent;
my $browser = LWP::UserAgent->new;

# And before you go making any requests:
$browser->env_proxy;

�����㏍�㏍�桁����違�̹�≪�������������ŝ�������宴�˨�ゃ����⓾�壔��LWP::UserAgent �� ������ ��勉����鴻�� proxy, env_proxy, no_proxy ��<�純�������������с����⓾��������������

HTTP Authentication

(HTTP 茯�荐�)

紊������勉�⓾�с����泣�ゃ����壩����吾�吾�勉�≪�壔�祉�鴻�� "HTTP 茯�荐�" ���篏帥�c�☗�狗�������⓾����障����� ��������壠����ŝ�� "�����鴻�壔�若�������ュ�������⓾�����������" 綵√����勐�狗����с�壔�ŝ����� HTTP ��泣�若����������í�⓾�吟�˨�������勖����吾�壚��茘激�������������㋘�˨�������勌�������с�������� ��ŝ�壔����鴻�����絨������勤��荐若�����������菴遵�������☗����ŝ�壔����鴻����������翫����˨�勉� ��≪�壔�祉�鴻�с����障�������������������潟�� HTTP ��潟�若�����菴����������������絨������勖��罕���с�����

箴������違��Unicode.org ��勛�∞�������壔����<�若�ŝ�潟�違�ŝ�鴻����≪�若�˨�ゃ����� HTTP 茯�荐若� 篆�茘激����⓾�������勉�⓾�若�吟�弱����������鴻�壔�若����� (http://www.unicode.org/mail-arch/ ���)��㋘�˨����� (��⓾�若�吟�弱�� "unicode-ml" �����鴻�壔�若�� "unicode") ��������с�� ��糸����<�若�˨�≪����㋘�劫�������������������≪�若�˨�ゃ����������≪����㋘�鴻����������������勉�� ��紙②�����⓾����障�����

箴������違����⓾�с����泣�ゃ����勉����>��茘激�������������ŝ�≪�с�������������� URL ��˨�ゃ���� ��������障��:

http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html

��������˨����í�⓾�吟�с�≪�壔�祉�鴻����������� "Enter username and password for 'Unicode-MailList-Archives' at server 'www.unicode.org'" ��勉�������ŝ����㏍�潟����������冴�障�����

LWP ��с�壔�������������˨����� URL ��˨�ŝ�壔����鴻�������������ŝ�����篁ヤ����勉�������˨����障��:

use LWP;
my $browser = LWP::UserAgent->new;

my $url =
 'http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html';
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;

��������������������勉����í�若����������������障��:

Error: Basic realm="Unicode-MailList-Archives"
 401 Authorization Required
 at http://www.unicode.org/mail-arch/unicode-ml/y2002-m08/0067.html
 Aborting at auth1.pl line 9.  [or wherever]

��ŝ����ŝ�� $browser ��壔����勉����鴻�� ("www.unicode.org") ��勉����勉�㋘�˨�� ("Unicode-MailList-Archives") ��勉�⓾�若�吟�弱����������鴻�壔�若�������ャ����ŝ����������с����� �����í�⓾�吟�˨����������ャ�������������c�����膂≦����ʋ�号����壔�������勉����鴻����勉����勉�㋘�˨����˩戎��� ��⓾�若�吟�若�������鴻�壔�若�������ャ�������� credentials ��<�純��������篏帥����������с����� ���羈����:

$browser->credentials(
  'servername:portnumber',
  'realm-name',
 'username' => 'password'
);

��祉�������í�勐�翫����������若����ɱ垩�� HTTP ��勉�������í�˨�� TCP/IP �����若����с����� 80 ��с��; ��������〠�勰����壔�ŝ�壔����鴻����勐����� credentials ��<�純����������若�喝�冴����障����� 箴�������:

$browser->credentials(
  'reports.mybazouki.com:80',
  'web_server_usage_reports',
  'plinky' => 'banjo123'
);

��������⓾��筝�菴違�勉����㏍�違�í����˩札筝���勉����勉�������������ŝ�����$browser = LWP::UserAgent->new; 茵���勛�翫����˨����障����

$browser->credentials(  # add this to our $browser 's "key ring"
  'www.unicode.org:80',
  'Unicode-MailList-Archives',
  'unicode-ml' => 'unicode'
);

������������絎�茵���������������ŝ�壔����鴻����� die ���綣����莎激����������������������障�����

Accessing HTTPS URLs

(HTTPS URL ��˨�≪�壔�祉�鴻�����)

HTTPS ��� URL ��˨�≪�壔�祉�鴻�����������(LWP ��ゃ�潟�鴻����若�������� (��ï����� SSL ��í�ゃ����í�ŝ��篏帥�c��) HTTPS 絲上�������⓾�������ŝ��) HTTP ��� URL ������罕�������篏������障����� 箴�������:

use LWP;
my $url = 'https://www.paypal.com/';   # 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";

HTTPS 絲上����ŝ����� LWP �����ゃ�潟�鴻����若�˨�������⓾�������翫�������㋘�鴻����潟�鴻�壠け��������� 篁ヤ����勉�������ŝ����í�若�<����祉�若�吾����榊�������障��:

Error at https://www.paypal.com/
 501 Protocol scheme 'https' is not supported
 Aborting at paypal.pl line 7.   [or whatever program and line]

LWP ��ゃ�潟�鴻����若�������� HTTPS 絲上����������� ������ ��翫�������㋘�鴻����潟�鴻�壩����������� ���絽吾�� HTTP ��㋘�鴻����潟�鴻�����罕���� $response ���篏帥����障�����

LWP ��ゃ�潟�鴻����若�������� HTTPS 絲上����˨�������������勖����宴�˨�ゃ����⓾�壔��libwww-perl ���絽��� ���罌宴�������⓾����� README.SSL �����<�ゃ�˨�������с����⓾��������������

Getting Large Documents

(紊с����ʋ����吾�����緇�������)

紊с����� (��障����壠����ŝ��������紊с������櫝�醇�с��������) �����吾����ŝ�壔����鴻����������������� ($response = $browser->get($url) ��勉��������) ��ŝ�壔����鴻����<�純��������篏帥�c�� ���絽吾�勖�号����勐�馹���鴻�壔����<�≪�ŭ����勉�㋘�鴻����潟�鴻�ŝ����吾�с�壔����������後��篏����篆���������� 綽�荀������������������с�� -- ��<�≪�ŭ�������� ��������㋘�鴻����潟�鴻�� 30 ��<�㋘����ゃ����勉����<�ゃ�˨�ŝ����������������������㏍�祉�鴻�勉�<�≪�ʋ��莢祉� �����c�☀����������ŝ����с�����������

羈◐�勉�������鴻��篁f�炊����壔��LWP �����<�≪�ŭ����с�壔�ŝ����������c�鴻�壚����勉����<�ゃ�˨�����絎鴻�� 篆�絖���������������˨�������������с����� ���������篏睡����������������勖��羈���с��:

$response = $ua->get($url,
                       ':content_file' => $filespec,
                    );

箴������違��

$response = $ua->get('http://search.cpan.org/',
                       ':content_file' => '/tmp/sco.html'
                    );

:content_file ��ŝ����激�с�潟��篏帥�����������$response ��˨�奝��絽吾�勉��������茵� �����⓾����˨�障����⓾����障��������$response->content ��夌Ã��с�����

������ ":content_file" ��ŝ����激�с�潟�壠𡉴�������若�吾�с�潟�� LWP ��с� 絲上�������⓾����ŝ����勉�с���������𡉴�������若�吾�с�潟�勉�激�鴻�������с��絎�茵������������������������ŝ���� �����������ŝ�����LWP �����若�吾�с�潟�������с����壔�������������� use LWP 5.66; ��� 菴遵����������������������勉�������鴻����������������障��������

�������𡉴�� LWP �����若�吾�с�潟����勌�������с��綽�荀���ŝ��������������������������篁ヤ����勖�������� 篏帥�c�⓾�����������:

use HTTP::Request::Common;
$response = $ua->request( GET($url), $filespec );

SEE ALSO

�����勤��篋���� LWP ��勖��������罩ñ����ʃą�����̹�������ŝ�����������羈���������⓾����������� -- LWP ��������� LWP ��∫�c�勉�帥�鴻�壔�˨�ゃ����⓾����c��絖⓾�吟�������˨�壔����㋚����˩札筝���勉����勉�� 茯㏍�障�ŝ�������違�ŝ����障�����:

  • LWP::Simple -- URL ��勐��緇����������������緇���������í�若�勉�������勐��膣���ʒ�∽�

  • LWP -- libwww-perl ��≪�吾�ャ�若�˨�勖��茯

  • LWP::UserAgent -- ���篁勖�潟����í�⓾�吟�����茵◐憗�������ŝ����吾�с�壔����勉�壔�í�

  • HTTP::Response -- $response = $browser->get(...) ��ŝ�í� LWP ��㋘�鴻����潟�鴻��茵◐憗�������������勉�ŝ����吾�с�壔����勉�壔�í�

  • HTTP::Message ��� HTTP::Headers -- HTTP::Response ��˨����c��紊������ ��<�純�����������箴���������壔�í�

  • URI -- 腟九嚳��������括�後嚳��� URL ���茵◐憗�������ŝ����吾�с�壔����勉�壔�í�

  • URI::Escape -- ("this & that" ��� "this%20%26%20that" ��勐�������勉��������) URL �����鴻�宴�若����� URL ��≪�潟����鴻�宴�若����勉�������勰�∽�

  • HTML::Entities -- ("C. & E. Brontë" ��� "C. &amp; E. Bront&euml;" �� 紊������勉��������) HTML �����鴻�宴�若����� HTML ��≪�潟����鴻�宴�若����勉�������勰�∽�

  • HTML::TokeParser ��� HTML::TreeBuilder -- HTML ��������若�鴻�������������勉�壔�í�

  • HTML::LinkExtor -- HTML �����娯賢��勉�ŝ�潟�壔���「�����壔�í�

  • Sean M. Burke ��˨�������悟�� Perl & LWP O'Reilly & Associates, 2002. ISBN: 0-596-00178-9. http://www.oreilly.com/catalog/perllwp/

COPYRIGHT

Copyright 2002, Sean M. Burke. You can redistribute this document and/or modify it, but only under the same terms as Perl itself.

AUTHOR

Sean M. Burke sburke@cpan.org