つぶねこ

@もじらもーど。
↑recent | ↓old | 20 index | 100 index | 300 index | 記事5件 | 記事10件 | 記事20件 |最新5件 |FAQ | カテゴリ一覧 | Index |
2010/12/24 7:31
さいきんのヤフオククローラー

 WWW::MechanizeとかWeb::Scraperのネタをつぶやいたので、ついでということで何か実装してみようという。
use LWP::Simple;
use LWP::UserAgent;
use WWW::Mechanize;
use utf8;
use Web::Scraper;
use Encode;

my $url = 'http://list3.auctions.yahoo.co.jp/jp/%E7%84%A1%E5%81%9C%E9%9B%BB%E9%9B%BB%E6%BA%90%E8%A3%85%E7%BD%AE-%E3%83%8D%E3%83%83%E3%83%88%E3%83%AF%E3%83%BC%E3%82%AF-%E5%91%A8%E8%BE%BA%E6%A9%9F%E5%99%A8-%E3%82%B3%E3%83%B3%E3%83%94%E3%83%A5%E3%83%BC%E3%82%BF/2084039702-category-leaf.html?price_type=currentprice&min=&max=100000&abatch=1&istatus=0&mode=2&p=&fo=&sb=&desc=&cat=&auccat=&f=&loc_cd=&min_point=&max_point=&auchours=&aucminutes=&e_wday=&e_time=&s_wday=&s_time=&filtered=1';
my $result = &get_list($url);
@$result = sort{ $b->{price} <=> $a->{price} || $a->{desc} cmp $b->{desc} }(@$result);
foreach my $r (@$result) {
next if($r->{price} > 40000);
my $str = &add_comma( $r->{price} ) . "\t"
. qq(<a href="$r->{uri}">) . $r->{desc} . "" . "\n";
print encode('utf-8' , $str );
}

sub get_list{
my $url = shift;
my $agent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50$
my $mech = WWW::Mechanize->new(agent=>$agent);
my @result;

$mech->get( $url );
push( @result, &scr( $mech->content ) );
my $re = qr/\Q次の25件\E/ ;
while( $mech->find_link('text_regex' => $re ) ){
$mech->follow_link( 'text_regex' => $re );
push( @result, &scr( $mech->content ) );
}
return \@result;
}

sub scr{
my $yahuoku2 = scraper {
process 'td.pad01>a' , uri => '@href' , 'desc' => 'TEXT';
process 'td.pad01>small>a' , 'owner' => sub{
my @list = $_->find("a");
return $list[0]->as_text;
};
process 'td.pad01>b' , 'price[]' => 'TEXT';
result 'uri','desc','owner','price';
};
my $yahuoku1 = scraper {
process 'div#list01>table>tr' , 'auctions[]' => $yahuoku2;
result 'auctions';
};

my $html = shift;
my $result = $yahuoku1->scrape( $html );
for (my $m=@$result ; $m>=0 ; $m--){
splice(@$result,$m,1) unless ( $$result[$m]->{uri} );
}
my @p;
foreach my $r (@$result){
@p = grep(/^ *[0-9,]+ *\Q円\E */ , @{$r->{price}} );
$r->{price} = $p[0];
$r->{price} =~ s/[^0-9]//g;
}
return @$result;
}
 これでヤフオクのUPSカテゴリーからタイトルと価格あたりをリストしてくる。
 scraperのhtmlの一部分を拾ってくる所は、何かもう少しやり方がありそうだが、とりあえず現状合わせ。CSSセレクタじゃにゃくてXPathのほうがいいのかしら? どうもそのあたりの書式でどこまで記述できるのかを知らずに書いてるので酷いことに。あとこういうタグベースじゃにゃくて、にゃんかこう、TEXT部分でサーチしてヒューリスティックに検出とかした方がいいんだろうにゃぁ、とか思いつつ、あぁそれってscraperじゃにゃくてMechanizeにゃんじゃにゃいの?という根本的にゃ・・・
 まぁ習学にゃのでいいとして
 これはhtmlぽいのを吐くのでcronとかで
 mail -s title -a "Content-type: text/html;" mail@mail.com
 で投げると便利。



Index


2011/01/25 08:13 
2011/01/24 16:21 
2011/01/23 15:59 
2011/01/23 00:15 
2011/01/22 19:29 
2011/01/21 21:23 
2011/01/20 11:31 
2011/01/19 09:41 
2011/01/18 10:25 
2011/01/17 09:24 
2011/01/16 04:59 
2011/01/15 22:06 
2011/01/14 20:32 
2011/01/13 20:26 
2011/01/12 11:59 
2011/01/12 01:56 
2011/01/11 04:36 
2011/01/11 00:01 
2011/01/10 07:42 
2011/01/10 00:05 
2011/01/09 13:03 
2011/01/09 00:06 
2011/01/08 23:34 
2011/01/08 00:46 
2011/01/07 20:57 
2011/01/07 04:50 
2011/01/06 21:22 
2011/01/06 04:44 
2011/01/05 13:06 
2011/01/05 04:46 
2011/01/04 21:06 
2011/01/04 04:43 
2011/01/03 14:46 
2011/01/03 00:11 
2011/01/02 19:48 
2011/01/02 00:12 
2011/01/01 19:06 
2011/01/01 09:48 
2010/12/31 23:45 
2010/12/30 11:00 
2010/12/30 00:53 
2010/12/29 21:00 
2010/12/28 00:50 
2010/12/27 12:34 
2010/12/27 01:22 
2010/12/26 11:48 
2010/12/26 01:16 
2010/12/25 16:29 
2010/12/25 01:37 
2010/12/24 15:35 
2010/12/24 07:31 
2010/12/23 18:08 
2010/12/23 01:40 
2010/12/22 21:26 
2010/12/22 01:18 
2010/12/21 12:23 
2010/12/21 01:29 
2010/12/20 20:20 
2010/12/20 00:52 
2010/12/19 17:50 
2010/12/19 00:13 
2010/12/18 13:01 
2010/12/18 01:39 
2010/12/17 19:30 
2010/12/17 09:00 
2010/12/16 19:28 
2010/12/16 09:01 
2010/12/15 21:36 
2010/12/15 08:57 
2010/12/14 21:03 
2010/12/14 08:53 
2010/12/13 18:08 
2010/12/12 21:56 
2010/12/11 09:15 
2010/12/10 22:43 
2010/12/10 02:07 
2010/12/09 21:41 
2010/12/09 00:36 
2010/12/08 18:34 
2010/12/07 02:12 
2010/12/06 17:48 
2010/12/06 03:11 
2010/12/05 23:13 
2010/12/05 00:41 
2010/12/04 23:38 
2010/12/04 03:06 
2010/12/03 22:23 
2010/12/03 01:26 
2010/12/02 22:16 
2010/12/02 01:15 
2010/12/01 21:31 
2010/12/01 01:30 
2010/11/30 14:04 
2010/11/30 01:02 
2010/11/29 20:46 
2010/11/29 01:54 
2010/11/28 11:27 
2010/11/28 01:10 
2010/11/27 12:06 
2010/11/27 01:21 
2010/11/26 15:14 

↑recent | ↓old | 20 index | 100 index | 300 index | 記事5件 | 記事10件 | 記事20件 |最新5件 |FAQ | カテゴリ一覧 | Index |