--- title: LWPをつかってみる author: kazu634 date: 2008-09-04 url: /2008/09/04/_1095/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4255;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - LWP - Perl ---
Gaucheの方はとりあえず頭の働く土日へ…
# === Libraries === use strict; use warnings; # LWP module use LWP 5.64; # Character Encoding use Encode; use utf8; binmode(STDERR, ':raw :encoding(utf8)'); my $url = 'http://www.cpan.org/RECENT.html'; # get then content of the url. my $browser = LWP::UserAgent->new; my $response = $browser->get( $url, 'User-Agent' => 'Mozilla/4.77 [en] (Win98; U)', 'Accept' => 'image/gif, image/x-xbitmap, image.jpeg, image.pjpeg, image/png, */*', 'Accept-Encoding' => 'gzip', 'Accept-Language' => 'ja,en', 'Accept-Charset' => 'iso-8859-1, *, utf8', ); die "$url を読み込めませんでした。", $response->status_line unless $response->is_success; die "HTMLを読み込んだはずなのに、", $response->content_type, "が返ってきました。" unless $response-> content_type eq 'text/html'; # decoding. # Note how to use "decode": # decode($content's character code, the target string) my $content = decode('shiftjis', $response->content); # ここで相対URLを絶対URLに変換している while ( $content =~ m/<A HREF=\"(.*?)\"/g ) { print(URI->new_abs( $1, $response->base ), "\n"); }
ここの
while ( $content =~ m/<A HREF=\"(.*?)\"/g ) { print(URI->new_abs( $1, $response->base ), "\n"); }
がポイントだよ。