--- title: LWPの使い方を習得中 author: kazu634 date: 2008-08-30 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:4245;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - LWP - Perl ---
最終目標は図書館蔵書検索をするスクリプトを作ること。
LWP::Simpleを使うよ:
# === Libraries === use strict; use warnings; # LWP & Encode modules use LWP::Simple; use utf8; use Encode; binmode(STDERR, ':raw :encoding(utf8)'); # === Main part === # Here set the url. my $url = "http://www.klnet.pref.kanagawa.jp/opac/OPP0200"; # get then content of the url. my $content = get($url); die "$url を読み込めませんでした" unless defined $content; # decoding. # Note how to use "decode": # decode($content's character code, the target string) $content = decode('utf-8', $content); print($content);
# === Libraries === use strict; use warnings; # LWP & Encode modules use LWP 5.64; use utf8; use Encode; binmode(STDERR, ':raw :encoding(utf8)'); # === Main part === # Here set the url. my $url = "http://www.klnet.pref.kanagawa.jp/opac/OPP0200"; # get then content of the url. my $browser = LWP::UserAgent->new; my $response = $browser->get($url); 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('utf8', $response->content); print $content;
use strict; use warnings; # LWP module use LWP 5.64; # Character Encoding use Encode; use utf8; binmode( STDERR, ':raw :encoding(utf8)' ); my $url = 'http://tv.yahoo.co.jp/vhf/kanagawa/realtime.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( 'euc-jp', $response->content ); print($content);
Spidering hacks―ウェブ情報ラクラク取得テクニック101選