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/SearchResult.pm

    r18142 r18428  
    11package Net::DMM::Scraper::SearchResult; 
    2 # 
    3 # 検索結果ページをスクレイピング 
    4 # 
    5  
    6 use base qw(Class::Accessor); 
    7 __PACKAGE__->mk_accessors( 
    8         qw( txt_list img_list title link ) 
    9 ); 
    102 
    113use strict; 
     
    135#use utf8; 
    146 
    15 use URI::Fetch; 
    16 use Cache::FileCache; 
    17 use Encode qw(encode decode); 
    18 use URI; 
    197use Web::Scraper; 
     8use Net::DMM::Util qw( _fetch _rm_space ); 
    209 
    21 our $VERSION = '0.0.2'; 
     10use base qw(Class::Accessor); 
     11__PACKAGE__->mk_accessors( 
     12    qw( txt_list img_list title link ) 
     13); 
    2214 
    2315sub new { 
    24         my ($class, $opt) = @_; 
    25         my $self = bless { 
    26                 url   => $opt->{url}, 
    27                 opt   => $opt->{opt}, 
    28                 title => [], 
    29                 link  => [], 
    30         }, $class; 
    31         $self->_scrape if $self->{url}; 
    32         $self; 
     16    my ($class, $opt) = @_; 
     17    my $self = bless { 
     18        url   => $opt->{url}, 
     19        opt   => $opt->{opt}, 
     20        title => [], 
     21        link  => [], 
     22    }, $class; 
     23    $self->_scrape if $self->{url}; 
     24    $self; 
    3325} 
    3426 
    3527sub _scrape { 
    36         my $self = shift; 
    37         my $url  = URI->new($self->{url}); 
    38         my $result = scraper { 
    39                 process '/html/body/table/tr/td[3]/table[3]/tr/td/table/tr/td[5]/a', 
    40                 'txt_list' => '@href'; 
    41                 process '/html/body/table/tr/td[3]/table[3]/tr/td/table/tr/td[6]/a', 
    42                 'img_list' => '@href'; 
    43                 process '/html/body/table/tr/td/table/tr/td/table/tr/td[2]/a', 
    44                 'title[]' => 'TEXT', 
    45                 'link[]'  => '@href'; 
    46         }->scrape($self->_fetch($url)); 
     28    my $self = shift; 
     29    my $result = scraper { 
     30        process '/html/body/table/tr/td[3]/table[3]/tr/td/table/tr/td[5]/a', 
     31        'txt_list' => '@href'; 
     32        process '/html/body/table/tr/td[3]/table[3]/tr/td/table/tr/td[6]/a', 
     33        'img_list' => '@href'; 
     34        process '/html/body/table/tr/td/table/tr/td/table/tr/td[2]/a', 
     35        'title[]' => 'TEXT', 
     36        'link[]'  => '@href'; 
     37    }->scrape($self->_fetch($self->{url})); 
    4738 
    48         foreach my $key (keys %$result){ 
    49                 if($key =~ /^(title|link)$/){ 
    50                         foreach my $i (@{$result->{$key}}){ 
    51                                 push(@{$self->{$1}}, $i); 
    52                         } 
    53                 }else{ 
    54                         $self->set($key, $self->_delete_space($result->{$key})); 
    55                 } 
    56         } 
    57 } 
    58  
    59 sub _delete_space { 
    60         my ($self, $str) = @_; 
    61         $str =~ s/^\s*(.*?)\s*$/$1/; 
    62         return $str; 
    63 } 
    64  
    65 sub _fetch { 
    66         my $self = shift || return; 
    67         my $url  = shift || return; 
    68         my $page = ''; 
    69         if($self->{opt}->{fetch}){ 
    70                 $page = URI::Fetch->fetch( 
    71                         $url, 
    72                         UserAgent => $self->{opt}->{ua}, 
    73                         Cache     => $self->{opt}->{fetch}->{cache}, 
    74                         NoNetwork => $self->{opt}->{fetch}->{nonetwork}, 
    75                 ); 
    76         }else{ 
    77                 $page = URI::Fetch->fetch( 
    78                         $url, 
    79                         UserAgent => $self->{opt}->{ua}, 
    80                 ); 
    81         } 
    82         return encode('utf8', decode('euc-jp', $page->content)); 
     39    foreach my $key (keys %$result){ 
     40        if($key =~ /^(title|link)$/){ 
     41            foreach my $i (@{$result->{$key}}){ 
     42                push(@{$self->{$1}}, $i); 
     43            } 
     44        }else{ 
     45            $self->set($key, $self->_rm_space($result->{$key})); 
     46        } 
     47    } 
    8348} 
    8449 
     
    9156Net::DMM::Scraper::SearchResult 
    9257 
     58=head1 METHOD 
    9359 
    94 =head1 VERSION 
     60=over 
    9561 
    96 This document describes Net::DMM::Scraper::SearchResult version 0.0.2 
     62=item new 
    9763 
    98  
    99 =head1 SYNOPSIS 
    100  
    101     use Net::DMM::Scraper::SearchResult; 
     64=back 
    10265 
    10366=head1 AUTHOR