|
Revision 17483, 1.2 kB
(checked in by lopnor, 5 years ago)
|
|
lang/perl/Google-Chart-DBIC: first commit
|
| Line | |
|---|
| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | use FindBin; |
|---|
| 4 | use charnames ':full'; |
|---|
| 5 | use lib "$FindBin::Bin/lib"; |
|---|
| 6 | use TestApp::Schema; |
|---|
| 7 | use Web::Scraper; |
|---|
| 8 | use URI; |
|---|
| 9 | use YAML; |
|---|
| 10 | |
|---|
| 11 | my $schema = TestApp::Schema->connect( |
|---|
| 12 | "dbi:SQLite:test.db" |
|---|
| 13 | ); |
|---|
| 14 | my $rs = $schema->resultset('Climate'); |
|---|
| 15 | $rs->delete_all; |
|---|
| 16 | |
|---|
| 17 | my $cities = scraper { |
|---|
| 18 | process '//th[@bgcolor="#add8e6"]', 'data[]' => sub { |
|---|
| 19 | my $city = shift; |
|---|
| 20 | (my $cityname = $city->as_text) =~ s/\N{IDEOGRAPHIC SPACE}+//g; |
|---|
| 21 | my @citydata = $city->findnodes('../following-sibling::tr[1]/td[@align="right"]/font'); |
|---|
| 22 | my @result = (); |
|---|
| 23 | |
|---|
| 24 | for my $month (0 .. 11) { |
|---|
| 25 | my @data = (); |
|---|
| 26 | for ($citydata[$month]->content_refs_list) { |
|---|
| 27 | next if ref $$_; |
|---|
| 28 | push @data, $$_; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | push @result, { |
|---|
| 32 | place => $cityname, |
|---|
| 33 | month => $month + 1, |
|---|
| 34 | average => $data[0] + 0, |
|---|
| 35 | high => $data[1] + 0, |
|---|
| 36 | low => $data[2] + 0, |
|---|
| 37 | rainfall => $data[3] + 0, |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | return @result; |
|---|
| 41 | }; |
|---|
| 42 | result 'data'; |
|---|
| 43 | }->scrape(URI->new('http://www.faminet.co.jp/d_guide/d_gnc/gnc1_3.html')); |
|---|
| 44 | |
|---|
| 45 | warn Dump $cities; |
|---|
| 46 | for (@$cities){ |
|---|
| 47 | $rs->create($_); |
|---|
| 48 | }; |
|---|