目次 †
内容 †
ベーシックな使い方 †
SSLの場合 †
- Crypt::SSLeayをインストールしておく必要があるが
- 手順自体はHTTPの時と同じ(モジュール指定も必要なし)
UserAgentの指定 †
- LWP::UserAgentのインスタンスにagentメソッドで指定
$ua->agent( 'Mozilla' );
POSTを使う場合 †
BASIC認証 †
Cookieの利用 †
作例:タイトル取得プログラム †
- 文字コード判別にEncode::Detectを使用
#!/usr/bin/perl -w
###================================================================
### 使い方
###================================================================
# URL一覧をlist.txtという名前で配置
###================================================================
### モジュール
###================================================================
use strict;
use warnings;
use utf8;
use Encode;
use LWP;
###================================================================
### 設定
###================================================================
### ファイル名
my $src_file = "list.txt";
my $out_file = "result.txt";
###================================================================
### 処理開始
###================================================================
### 入力・出力ファイルを開く
open( my $fh_in, $src_file ) || die( "can't open src file: $!" );
open( my $fh_out, ">", $out_file ) || die( "Can't open result file: $!" );
### 処理開始
my $ua = LWP::UserAgent->new();
while( <$fh_in> ){
chomp;
next unless $_;
### URLでなければそのまま出力
if( ! m!^https?://! ){
print $_ . "\tng\tnot url\n";
next;
}
### URLアクセス
my $res = $ua->get( $_ );
if( $res->is_success ){
my $title_utf8 = decode( $res->content_charset, $res->title );
print $fh_out $_ . "\tok\t" . encode( "utf8", $title_utf8 ) . "\n";
} else {
print $fh_out $_ . "\tng\t" . $res->status_line . "\n";
}
}
### ファイルハンドルを閉じて終了
close $fh_in;
close $fh_out;
exit;
|