#!/usr/bin/perl -w ################################################################################ # url_getモジュール # HTTPアクセスを行い、取得したデータを配列で返す。 # # 使い方:括弧内はデフォルト値 # use url_get; # 宣言 # my $test = new url_get( # "http://hogehoge/hoge.html", # url # "GET", # GET or POST(GET) # "http://hogehoge:8080", # proxy (なし) # ); # my @htmls = $test->geturl(); # 配列取得 # # 文字コードは次から選択 # shiftjis/euc-jp/utf8 ################################################################################ package url_get; use LWP::Simple; use LWP::UserAgent; use HTTP::Request::Common qw(GET); use HTTP::Request::Common qw(POST); # グローバル変数 my $debug = "0"; # 1でurl_get.logに出力 my $url = ""; my $method = ""; my $proxy = ""; # new時にグローバル変数に設定 sub new { my $this = shift; ( $url, $method, $proxy ) = @_; # メソッドはデフォルト"GET" if ( !$method ) { $method = "GET"; } # プロキシはデフォルトなし if ( !$proxy ) { $proxy = ""; } # 文字コードはデフォルト"shift-jis" if ( !$charcode ) { $charcode = "shift-jis"; } bless {}, $this; } # 設定値の読み込み sub getoption { my @option = ( $url, $method, $proxy ); return @option; } # URLの内容を配列でGET sub geturl { # UserAgent 作成 my $ua = new LWP::UserAgent; # $proxyが設定されていたら使用する if ( $proxy ne "" ) { $ua->proxy('http', $proxy); } # GET or POST 設定 # $methodが設定されていなければGET my $request; if ( $method eq 'POST' ) { $request = POST($url); } else { $request = GET($url); } # URLのHTMLデータを取得 my $response = $ua->request( $request ); my @lines = split(/\n/, $response->content); my $html; for (@lines) { $_ =~ tr/\x0D\x0A|\x0D|\x0A|\x09//d; $html.=$_; } # 取得したHTMLデータの正規化 my $mode=0; my $line=0; my $str = 0; my $end = 0; my $i; my $char; for ( $i=0; $i" ) { $end = $i; $_ = substr( $html, $str, $end-$str+1 ); $lines[$line++]=$_ if ( ! /^[ \t]*$/ ); $str = $i+1; $mode = 0; } } } # デバッグ時は"url_get.log"に出力 if ( $debug eq "1" ) { open( File1, ">> url_get.log" ); print File1 qq|=== Start output geturl source ===\n|; for (@lines) { print File1 qq|$temp\n|; } print File1 qq|=== End output geturl source ===\n|; close( File1 ); } return @lines; } 1;