
一 LWP::Simple 功能
1. 如何在Perl中使用该模块?
| use LWP::Simple; |
| my $content = get(’http://www.yahoo.com.cn’); |
3. 如何获取头(Header)?
| my (b, d, $e) = header(’http://www.yahoo.com.cn’); |
4. 如何输出指定页面内容?
| my $code = getprint(’http://www.yahoo.com.cn’); |
5. 如何把获取的内容保存到一个文件中?
| my $code = getstore(’http://www.yahoo.com.cn’, ‘/path/file.html’); |
6. 如何同步远程和本地文件?
| my $code = mirror(’http://www.yahoo.com.cn’,'/path/file.html’); |
7. 如何测试返回状态的正确性?
is_success($code)
| is_error($code) |
二 LWP::UserAgent 功能
1、取得页面头信息
#!/usr/bin/perluse strict;
use warnings;
use LWP::UserAgent;my $ua = LWP::UserAgent->new;
$ua->agent(’spider name’);
my $res = $ua->head(’http://www.yahoo.com.cn’);
| foreach ($res->header_field_names) { print “$_: “, $res->header($_), “\\n”;} |
my $ua = LWP::UserAgent->new;
$ua->agent(’spider name’);
| my $response = $ua->get(’http://www.yahoo.com.cn’); |
use strict;
use warnings;
use LWP 5.;
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’; |
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); |
需要安装Crypt::SSLeay协议,以便支持加密传输。
命令行PPM下的安装:
ppm> installhttp://theoryx5.uwinnipeg.ca/ppms/Crypt-SSLeay.ppd
图形化下面的安装:
点击Edit->Preferences, Add Repository,添加http://theoryx5.uwinnipeg.ca/ppms/作为安装源。再选择Crypt-SSLeay即可。
测试代码:
use strict;
use warnings;
use LWP::UserAgent;my $url = ‘https://www.helsinki.fi/’;my $ua =LWP::UserAgent->new;
my $response = $ua->get( $url);$response->is_success or
die “Failedto GET ‘$url’: “, $response->status_line;
print $response->as_string;
查看文档来源:http://blog.sina.com.cn/s/blog_4af3f0d20100fwi0.html
