amazonから"ruby"に一致する検索結果の全ページを取得するプログラム

ソース

require 'rubygems'
require 'amazon/ecs'

Amazon::Ecs.options = {
  :aWS_access_key_id => ["ここにアクセスキーIDを設定"],
  :associate_tag => "ここにアソシエイトIDを設定",
  :country => :jp
}

#デバッグを有効、アクセスURLが表示される
#Amazon::Ecs.debug = true

page = 1
while true do 
  res = Amazon::Ecs.item_search('ruby', {:item_page => page, :response_group => 'Medium', :sort => 'salesrank'})
  print "#---------- #{page} ----------#\n"
  
  print "res.is_valid_request? : #{res.is_valid_request?}\n"
  print "res.has_error? : #{res.has_error?}\n"
  print "res.error :" #{res.error}\n"
  print "res.total_pages : #{res.total_pages}\n"
  print "res.total_results : #{res.total_results}\n"
  print "res.item_page : #{res.item_page}\n"
  print "res.items.size : #{res.items.size}\n"
  
  res.items.each do |item|
    print "asin : #{item.get('asin')}\n"
    print "itemattributes/title : #{item.get('itemattributes/title')}\n"
    
    atts = item.search_and_convert('itemattributes')
    print "title : #{atts.get('title')}\n"
    print "author : #{atts.get('author')}\n"
  end
  
  # 1秒間に1リクエストの制限があるので、取りあえず1秒待つ
  sleep(1)
  page = page + 1
  if res.total_pages < page then
   break
  end

end

プログラムの実行結果

#---------- 1 ----------#
res.is_valid_request? : true
res.has_error? : false
res.error :res.total_pages : 11
res.total_results : 110
res.item_page : 1
res.items.size : 10
asin : B001ACG1TQ
itemattributes/title : The Ruby (ザ・ルビー) 2008年 07月号 [雑誌]
title : The Ruby (ザ・ルビー) 2008年 07月号 [雑誌]
author : 
asin : 4844325795
itemattributes/title : まるごと Ruby! Vol.1
title : まるごと Ruby! Vol.1
author : るびきち
asin : 4839928266
itemattributes/title : Ruby on Rails 逆引きクイックリファレンス Rails 2.0対応
title : Ruby on Rails 逆引きクイックリファレンス Rails 2.0対応
author : 大場 寧子
asin : 4797336617
itemattributes/title : たのしいRuby 第2版 Rubyではじめる気軽なプログラミング
title : たのしいRuby 第2版 Rubyではじめる気軽なプログラミング
author : 高橋 征義
asin : 477751353X
itemattributes/title : はじめてのRuby on Rails 2―話題の「Webアプリケーション・フレームワーク」が使える! 便利なフレームワーク (2) (I/O BOOKS)
title : はじめてのRuby on Rails 2―話題の「Webアプリケーション・フレームワーク」が使える! 便利なフレームワーク (2) (I/O BOOKS)
author : 清水 美樹
asin : 4798114723
itemattributes/title : 10日でおぼえる Ruby on Rails入門教室
title : 10日でおぼえる Ruby on Rails入門教室
author : arton
asin : 4798115339
itemattributes/title : Ruby Way 第2版 (Professional Ruby Series)
title : Ruby Way 第2版 (Professional Ruby Series)
author : Hal Fulton
asin : 4873113539
itemattributes/title : RESTful Webサービス
title : RESTful Webサービス
author : Leonard Richardson
asin : 4839922217
itemattributes/title : Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~
title : Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~
author : Plan de Sens
asin : 4873113563
itemattributes/title : Rubyスクリプティングテクニック ―テスト駆動による日常業務処理術
title : Rubyスクリプティングテクニック ―テスト駆動による日常業務処理術
author : Brian Marick
#---------- 2 ----------#
res.is_valid_request? : true
res.has_error? : false
res.error :res.total_pages : 11
res.total_results : 110
res.item_page : 2
res.items.size : 10
asin : 4274066428
itemattributes/title : プログラミングRuby 第2版 言語編
title : プログラミングRuby 第2版 言語編
author : Dave Thomas
asin : 4274066436
itemattributes/title : プログラミングRuby 第2版 ライブラリ編
title : プログラミングRuby 第2版 ライブラリ編
author : Dave Thomas
asin : 4797340045
itemattributes/title : Rubyレシピブック 第2版 268の技
title : Rubyレシピブック 第2版 268の技
author : 青木 峰郎
asin : 4777512924
itemattributes/title : Rubyではじめるゲームプログラミング―人気の国産言語で、誰でも簡単にゲームが作れる! (I/O BOOKS)
title : Rubyではじめるゲームプログラミング―人気の国産言語で、誰でも簡単にゲームが作れる! (I/O BOOKS)
author : 山本 団
asin : 4844324780
itemattributes/title : 基礎Ruby on Rails (IMPRESS KISO SERIES)
title : 基礎Ruby on Rails (IMPRESS KISO SERIES)
author : 黒田 努
asin : 4839926689
itemattributes/title : Rubyist Magazine出張版 Ruby on Windows
title : Rubyist Magazine出張版 Ruby on Windows
author : cuzic
asin : 4756136877
itemattributes/title : Rubyを256倍使うための本 極道編
title : Rubyを256倍使うための本 極道編
author : 助田 雅紀
asin : 4798019437
itemattributes/title : はじめてのRubyプログラミング (TECHNICAL MASTER (52))
title : はじめてのRubyプログラミング (TECHNICAL MASTER (52))
author : 山本 団
asin : 4756137091
itemattributes/title : Rubyを256倍使うための本 無道編
title : Rubyを256倍使うための本 無道編
author : 青木 峰郎
asin : 4797341882
itemattributes/title : Ruby on RailsによるWebアプリケーション・スーパーサンプル
title : Ruby on RailsによるWebアプリケーション・スーパーサンプル
author : 久保秋 真
 ・
 ・
 ・
 ・
 ・
 ・
 ・
 ・
#---------- 11 ----------#
res.is_valid_request? : true
res.has_error? : false
res.error :res.total_pages : 11
res.total_results : 110
res.item_page : 11
res.items.size : 10
asin : 0340178566
itemattributes/title : Wallflower Ruby M Ayres
title : Wallflower Ruby M Ayres
author : Ayres
asin : 4770024193
itemattributes/title : ジキル博士とハイド氏 [Ruby books] 講談社ルビー・ブックス (ルビ訳)
title : ジキル博士とハイド氏 [Ruby books] 講談社ルビー・ブックス (ルビ訳)
author : ロバート・ルイス スティーブンソン
asin : 0671857800
itemattributes/title : Ruby/All That Glitters Poster
title : Ruby/All That Glitters Poster
author : Double Sided Poster
asin : 4904305051
itemattributes/title : 智場 #110
title : 智場 #110
author : 田中 辰雄
asin : 0521512808
itemattributes/title : KJV Royal Ruby Confirmation Bible Black French Morocco, T60 2380 CB
title : KJV Royal Ruby Confirmation Bible Black French Morocco, T60 2380 CB
author : 
asin : B000T7HGR4
itemattributes/title : 日経ソフトウエア 2007年 09月号 [雑誌]
title : 日経ソフトウエア 2007年 09月号 [雑誌]
author : 
asin : 4774117986
itemattributes/title : Rubyシェルプログラミング
title : Rubyシェルプログラミング
author : るびきち
asin : 4833720450
itemattributes/title : 看護理論とモデル (HBJ看護サマリー・シリーズ)
title : 看護理論とモデル (HBJ看護サマリー・シリーズ)
author : ルビー・L ウェズレイ
asin : 0322034787
itemattributes/title : Ruby Bridges/SSN/h/LP
title : Ruby Bridges/SSN/h/LP
author : 
asin : 4833720191
itemattributes/title : 看護理論とモデル (HBJ看護サマリー・シリーズ)
title : 看護理論とモデル (HBJ看護サマリー・シリーズ)
author : ルビー・L. ウェズレイ