Created by
sironekotoro
| #!/usr/bin/env perl
use strict;
use warnings;
use Encode qw/encode_utf8/;
use URI qw/new/;
use Web::Scraper qw/scrape/;
# Yahoo!Japan占いから12星座ランキングを取得して表示する(Web::Scraper版)
# 要Web::Scraper モジュールのインストール
# 星占いのリンクを元にURIオブジェクトを作成する
my $uri = URI->new('https://fortune.yahoo.co.jp/12astro/ranking.html');
# 星占いのページの星座の文字列はない・・・ので、画像タグの
# alt属性に書いてあるimgタグ内のalt属性に記載されている
# 星座名を取ってくることにする
#
# ex)
# <td class="st02 seiza-bg01"><p class="seiza"><img src="https://s.yimg.jp/images/fortune/sco42_s.gif" alt="さそり座">
# スクレイピングの前準備。XPathで取得したいタグ、属性を設定する
my $scraper = scraper {
# スクレイピング結果はrankという配列リファレンスに格納される
process '//td/p[@class="seiza"]/img', 'rank[]' => '@alt';
};
# URIオブジェクトを渡して、スクレイピング実行
my $res = $scraper->scrape($uri);
# $resの中にある配列リファレンスをデリファレンスして
# for文で順番に表示する
# スクレイピングして取得した文字列は内部文字列なので、
# エンコードして表示する
# constellation : 星座
for my $constellation ( @{ $res->{rank} } ) {
print encode_utf8($constellation), "\n";
}
|