Skip to content

Instantly share code, notes, and snippets.

@eru
Created June 23, 2012 07:24
Show Gist options
  • Save eru/2977337 to your computer and use it in GitHub Desktop.
Save eru/2977337 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Encode;
use WWW::Mechanize;
use Web::Scraper;
use YAML;
# 取得したい場所のYahoo!天気情報のURL
my $location_url = 'http://weather.yahoo.co.jp/weather/jp/20/4820/20215.html';
# 初期化,取得
my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->get($location_url);
# スクレイピング設定
my $scraper = scraper {
process 'h1.yjM', 'location' => ['TEXT', sub { s/の天気//o; } ];
process 'h3 > span.yjSt', 'date[]' => ['TEXT', sub { s/\s-\s//o; } ];
process 'table.yjw_table2', 'day[]' => scraper {
process 'tr', 'data[]' => scraper {
process 'td', 'stat[]' => 'TEXT';
}
};
};
# スクレイピング
my $weather_data = $scraper->scrape($mech->content());
# 場所
print encode_utf8($weather_data->{location}."\n");
# 天気情報をまとめて表示
for (my $i = 0; $i < @{$weather_data->{date}}; $i++) {
# 日付
print encode_utf8($weather_data->{date}[$i] . "\n");
# 天気情報
foreach my $day (@{$weather_data->{day}[$i]->{data}}) {
foreach my $item (@{$day->{stat}}) {
print encode_utf8($item.',');
}
print encode_utf8("\n");
}
print encode_utf8("\n");
}
# Debug
#print encode_utf8(YAML::Dump($weather_data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment