Webデータ取得(HTTPヘッダー, HTMLソース, use HTTP::Request::Common)
#! /usr/local/bin/perl use LWP::UserAgent; use HTTP::Request::Common; $url = "http://webings.net/sample/"; #Webデータ取得先アドレス print qq(Content-type: text/html; charset=Shift_JIS\n\n); print <<END; <html> <head> <title>Webデータ取得</title> </head> <body> <form action="" method="POST" style="width:center;text-align:center"> Webデータ取得<br> <textarea cols="80" rows="20"> END print getData($url); #Webデータ取得 print <<END; </textarea> </form> </body> </html> END exit; #============================================================================== Webデータ取得 sub getData { my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)'); my $request = GET($_[0]); my $response = $ua->request($request); return $response->content; # return $response->as_string; #HTTPヘッダーも取得の場合 # return $response->header('Content-Length'); #HTTPヘッダーの一部取得(ヘッダ名指定)の場合 }
〔 実行する 〕