Created
June 19, 2012 01:36
-
-
Save eru/2951824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Encode; | |
use WWW::Mechanize; | |
use Web::Scraper; | |
# 取得したい場所のYahoo!天気情報のURL | |
my $location_url = 'http://weather.yahoo.co.jp/weather/jp/8/4010.html'; | |
# 初期化,取得 | |
my $mech = WWW::Mechanize->new(autocheck => 1); | |
$mech->get($location_url); | |
# スクレイピング設定 | |
my $scraper = scraper { | |
process 'h1.yjM', 'location' => 'TEXT'; | |
process 'table.yjw_table[cellpadding="4"]', 'day[]' => scraper { | |
process 'table > tr > td > b', 'date' => 'TEXT'; | |
process 'table > tr > td > img', 'weather' => '@alt'; | |
process 'table > tr > td > small > font[color="#ff3300"]', 'temp_max' => 'TEXT'; | |
process 'table > tr > td > small > font[color="#0066ff"]', 'temp_min' => 'TEXT'; | |
process 'table.yjw_table3_s > tr[bgcolor="#e9eefd"] > td[width="18%"]', 'precipitation[]' => 'TEXT'; | |
process 'table.yjw_table3_s > tr[bgcolor="#eeeeee"] > td[width="18%"]', 'time_band[]' => 'TEXT'; | |
}; | |
}; | |
# スクレイピング | |
my $weather_data = $scraper->scrape($mech->content()); | |
# 現在地 | |
print encode_utf8($weather_data->{location} . "\n"); | |
# 日ごとに | |
foreach my $item (@{$weather_data->{day}}) { | |
# 日付 | |
print encode_utf8($item->{date} . "\n"); | |
# 天気 | |
print encode_utf8($item->{weather} . "\n"); | |
# 最高気温 | |
if ($item->{temp_max} =~ /.*\](\d+)\s\[\s(.*\d+)\s\].*/o) { | |
# 気温 | |
print encode_utf8($1 . ', '); | |
# 前日比 | |
print encode_utf8($2 . "\n"); | |
} | |
# 最低気温 | |
if ($item->{temp_min} =~ /.*\](\d+)\s\[\s(.*\d+)\s\].*/o) { | |
# 気温 | |
print encode_utf8($1 . ', '); | |
# 前日比 | |
print encode_utf8($2 . "\n"); | |
} | |
# 時間帯と降水確率 | |
for (my $cnt = 0; $cnt < @{$item->{time_band}}; $cnt++) { | |
# 時間帯 | |
print encode_utf8($item->{time_band}[$cnt]); | |
# コロンで区切る | |
print encode_utf8(' : '); | |
# 降水確率 | |
print encode_utf8($item->{precipitation}[$cnt] . "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment