今回はXPathを使ったよ。
nhk_scraper.pl
#! /usr/bin/perl use strict; use warnings; use Web::Scraper; use URI; use YAML; my $uri = 'http://www.nhk.or.jp/special/schedule/index.html'; my %nhk_scraper; $nhk_scraper{'tweek'} = scraper { process '//table[@class="tweek"]', 'tweek[]' => scraper { process '//td[2]', date => 'TEXT'; process '//td[3]/node()[1]', time_ch => sub{$_->string_value;}; process '//a[@class="tweektitle"]', title => 'TEXT'; }; result qw/tweek/; }; $nhk_scraper{'future'} = scraper { process '//table[@width="891"]', 'fweek[]' => scraper { process '//td[2]', date => 'TEXT'; process '//td[3]', title => 'TEXT'; }; result qw/fweek/; }; $nhk_scraper{'all'} = scraper { process '//table[@width="935"]', 'programs[]' => scraper { # this week process '//div[@id="sche"]', 'this_week' => $nhk_scraper{tweek}; # future process '//div[@id="sche2"]', 'future_week' => $nhk_scraper{future}; }; result qw/programs/; }; warn YAML::Dump( $nhk_scraper{'all'}->scrape( URI->new($uri) ) );
それで、NHKスペシャルの放送予定を取り出してみた。
--- - {} - this_week: - date: 10月1日(月) time_ch: 午後10時〜10時49分 総合 title: ' 地域の医療はよみがえるか 〜夕張からの報告〜' - date: 10月7日(日) time_ch: 午後9時〜9時49分 総合 title: ' 激流中国 チベット 聖地に富を求めて' - future_week: - date: 10月8日(月) title: ' 北朝鮮帰国船 〜知られざる半世紀の記録〜' - date: 10月14日(日) title: ' ライスショック 第1回 ‘食料小国’ニッポン 〜新潟・コシヒカリ〜(仮)' - date: 10月15日(月) title: ' ライスショック 第2回 改革元年 生産基地の実験場 〜秋田・アキタコマチ〜(仮)' - date: 10月20日(土) title: ' 日本の、これから どうする?私たちの主食(仮)' - date: 10月21日(日) title: ' 陸軍・特攻隊とは何だったのか 〜学徒兵たちの戦争〜(仮)' - date: 10月22日(月) title: ' 100年の難問はなぜ解けたのか 〜天才数学者 失踪の謎〜(仮)' - date: 10月28日(日) title: ' 新シルクロード 激動の大地をゆく(全7集) 第6集 希望の門はるか 〜トルコ〜(仮)'
XPath、いいですね。
というか、Web::Scraper、楽しいです(2回目)。