Show
Ignore:
Timestamp:
08/29/08 16:42:16 (4 months ago)
Author:
bayashi
Message:

いろいろと手を加えました

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Net-DMM/trunk/lib/Net/DMM/Scraper/PcgamesCaptureImg.pm

    r18142 r18428  
    11package Net::DMM::Scraper::PcgamesCaptureImg; 
    2 # 
    3 # pcgameカテゴリの個別商品ページからキャプチャ画像をスクレイピング 
    4 # 
     2 
     3use strict; 
     4use warnings; 
     5#use utf8; 
     6 
     7use Web::Scraper; 
     8use Net::DMM::Util qw( _fetch _rm_space ); 
    59 
    610use base qw(Class::Accessor); 
    711__PACKAGE__->mk_accessors( 
    8         qw( title img ) 
     12    qw( title img ) 
    913); 
    1014 
    11 use strict; 
    12 use warnings; 
    13 use utf8; 
    14  
    15 use URI::Fetch; 
    16 use Cache::FileCache; 
    17 use Encode qw(encode decode); 
    18 use URI; 
    19 use Web::Scraper; 
    20  
    21 our $VERSION = '0.0.2'; 
    22  
    2315sub new { 
    24         my ($class, $opt) = @_; 
    25         my $self = bless { 
    26                 url   => $opt->{url}, 
    27                 opt   => $opt->{opt}, 
    28                 img   => [], 
    29         }, $class; 
    30         $self->_scrape if $self->{url}; 
    31         $self; 
     16    my ($class, $opt) = @_; 
     17    my $self = bless { 
     18        url   => $opt->{url}, 
     19        opt   => $opt->{opt}, 
     20        img   => [], 
     21    }, $class; 
     22    $self->_scrape if $self->{url}; 
     23    $self; 
    3224} 
    3325 
    3426sub _scrape { 
    35         my $self = shift; 
    36         my $url  = URI->new($self->{url}); 
    37         my $result = scraper { 
    38                 process '/html/body/table/tr/td[3]/div/p/strong', 
    39                 'title' => 'TEXT'; 
    40                 process '/html/body/table/tr/td[3]/table/tr/td/input', 
    41                 'img[]' => '@src'; 
    42         }->scrape($self->_fetch($url)); 
     27    my $self = shift; 
     28    my $result = scraper { 
     29        process '/html/body/table/tr/td[3]/div/p/strong', 
     30        'title' => 'TEXT'; 
     31        process '/html/body/table/tr/td[3]/table/tr/td/input', 
     32        'img[]' => '@src'; 
     33    }->scrape($self->_fetch($self->{url})); 
    4334 
    44         foreach my $key (keys %$result){ 
    45                 if($key =~ /^(img)$/){ 
    46                         foreach my $i (@{$result->{$key}}){ 
    47                                 push(@{$self->{$1}}, $i); 
    48                         } 
    49                 }else{ 
    50                         $self->set($key, $self->_delete_space($result->{$key})); 
    51                 } 
    52         } 
    53 } 
    54  
    55 sub _delete_space { 
    56         my ($self, $str) = @_; 
    57         $str =~ s/^\s*(.*?)\s*$/$1/; 
    58         return $str; 
    59 } 
    60  
    61 sub _fetch { 
    62         my $self = shift || return; 
    63         my $url  = shift || return; 
    64         my $page = ''; 
    65         if($self->{opt}->{fetch}){ 
    66                 $page = URI::Fetch->fetch( 
    67                         $url, 
    68                         UserAgent => $self->{opt}->{ua}, 
    69                         Cache     => $self->{opt}->{fetch}->{cache}, 
    70                         NoNetwork => $self->{opt}->{fetch}->{nonetwork}, 
    71                 ); 
    72         }else{ 
    73                 $page = URI::Fetch->fetch( 
    74                         $url, 
    75                         UserAgent => $self->{opt}->{ua}, 
    76                 ); 
    77         } 
    78         return encode('utf8', decode('euc-jp', $page->content)); 
     35    foreach my $key (keys %$result){ 
     36        if($key =~ /^(img)$/){ 
     37            foreach my $i (@{$result->{$key}}){ 
     38                push(@{$self->{$1}}, $i); 
     39            } 
     40        }else{ 
     41            $self->set($key, $self->_rm_space($result->{$key})); 
     42        } 
     43    } 
    7944} 
    8045 
     
    8752Net::DMM::Scraper::PcgamesCaptureImg 
    8853 
     54=head1 METHOD 
    8955 
    90 =head1 VERSION 
     56=over 
    9157 
    92 This document describes Net::DMM::Scraper::PcgamesCaptureImg version 0.0.2 
     58=item new 
    9359 
    94  
    95 =head1 SYNOPSIS 
    96  
    97     use Net::DMM::Scraper::PcgamesCaptureImg; 
     60=back 
    9861 
    9962=head1 AUTHOR