ヤフーオークションの検索結果の全ページから画像とってきてhtmlつくるrubyスクリプト

物欲を満たすためではなく、満たせない物欲を慰めるために。
つくってみたらまるでなぐさめられなかった。がんばって働こう。



ruby YahooAuctions.rb test.yaml

とすればこんなhtmlができあがります。

別にワンフェスをチェックしてるわけではありません。きれいなジャイアンくらいしか気になってません。


マウスオーバーで画面の左上にタイトル、右上に価格。たぶんIE6だとくずれる。
検索結果のページ数が多いとものすごく時間かかるし、画像も全部表示されない。javascriptとかでがんばるところなのかもしれないけどよく知らない。
長辺あわせの画像縮小のしかたもわからなかった。


スーパープレ記法をはじめてしった。

YahooAuctions.rb

require 'rubygems'
require 'mechanize'
require 'hpricot'
require 'open-uri'
require 'kconv'
require 'yaml'

class GetDetailYahooAuctions
  def initialize(keywords,auccat)
    @keywords = keywords
    @auccat = auccat
    @agent = WWW::Mechanize.new
    page = @agent.get('http://auctions.yahoo.co.jp/jp/')
    search_form = page.forms.first
    search_form.p = @keywords.toeuc
    search_form.auccat = @auccat
    @search_results = @agent.submit(search_form)
    @@hash = Hash.new
    @nextpage = ““
  end

  def get_nextpage
    @nextpage = nil
    @search_results.links.each do |nextpage|
      if /次の.*/ =~ nextpage.text.toutf8
        @nextpage = nextpage
        break
      end
    end
  end

  def click_nextpage
    @search_results = @agent.click(@nextpage)
  end

  def get_links
    @search_results.links.each do |link|
      if /\/jp\/auction\// =~ link.href && /\n\n/ !~ link.text
        @@hash[link] = ““
      end
    end
    @@hash
  end

  def get_detail
    i = 0
    @@hash.each_key{|link|
      hash = Hash.new
      get_image(link)
      get_title_price(link)
      hash = {“price“ => @price, “title“ => @title, “images“ => @images}
      @@hash[link] = hash
    }
  end 

  def get_image(link)
    @images = Array.new
    allimg = Hpricot(open(link.href).read).get_elements_by_tag_name(“img“)
    allimg.each do |detailimg|
      if /(<img.*\/users\/.*>)/ =~ detailimg.to_s
        @images << $1
      end
    end
  end

  def get_title_price(link)
    allfont = Hpricot(open(link.href).read).get_elements_by_tag_name(“font“)
    allfont.each do |tp|
      case
      when /(<font.*\+1.*000000.*<b>)(.*)(<\/b>.*)/i =~ tp.to_s
        @title = $2
      when /(<font.*000000.*><b>)(.*\261\337)(<.*font>)/i =~ tp.to_s
        @price = $2
        break @price
      end
    end
  end

  def publish_html
    a = Array.new
    a.push('<html><head><link rel=“stylesheet“ href=“YahooAuctions.css“ type=“text/css“ /></head><body>')
    i = 0
    @@hash.each_pair {|key, value|
      a.push('<a href=“', key.href, '“ target=“_blank“>')
      a.push('<span class=“title“>', value[“title“], '</span>')
      a.push('<span class=“price“>', value[“price“], '</span>')
      value[“images“].each do |img|
        a << img
      end
      a << '</a>'
    }
    a << “</body></html>“
    publish_html = @keywords + '_' + @auccat.to_s + '.html'
    file = File.open(publish_html, 'w')
    a.each do |l|
      file.print l
    end
    file.close
  end

  def go
    print “search “ + @keywords + “ / “ + @auccat.to_s + “\n“
    i = 0
    loop {
      i+=1
      get_links
      get_nextpage
      print ' ', i,“page...“,“\n“
      if @nextpage
        click_nextpage
      else
        break
      end
    }
    print “ get detail...\n“
    get_detail
    print “ publish html...\n“
    publish_html
    print “owata\n“
  end

end

str = ARGF.read()
data = YAML.load(str)
data.each do |set|
  a = GetDetailYahooAuctions.new(set[“keywords“],set[“auccat“])
  a.go
end

なんか素敵なコードではない気がするのだけどなぜかはよくわからないので、先は長い。
imgとってくるのはwatch_for_setがよいのだと思うのに使い方が理解できなくて挫折。


test.yaml

- keywords: ワンフェス
  auccat: 25464
- keywords: きれいなジャイアン
  auccat: 0
# - keywords: 検索する単語
#   auccat:  検索カテゴリの数字。

# 0: すべてのオークション
# 23632: 家電、AV、カメラ
# 24698: スポーツ、レジャー
# 23336: コンピュータ
# 26318: 自動車、オートバイ
# 26084: その他
# 25464: おもちゃ、ゲーム
# 24242: ホビー、カルチャー
# 24198: 住まい、インテリア
# 24202: ベビー用品
# 23976: 食品、飲料
# 42177: ビューティー、ヘルスケア
# 21600: 本、雑誌
# 23140: アクセサリー、時計
# 23000: ファッション
# 20060: コミック、アニメグッズ
# 20000: アンティーク、コレクション
# 22896: 事務、店舗用品
# 22152: 音楽
# 21964: 映画、ビデオ
# 2084032594: タレントグッズ
# 2084043920: チケット、金券、宿泊予約
# 2084060731: ペット、生き物
# 2084217893: 不動産
# 2084217893: チャリティー
# サブカテゴリは2084224580-category-leaf.html?とかなってるの数字使えばよい模様。パン屑とかから確認できる。

YahooAuctions.css

body {
  background-color: #232323;
}
img {
  max-width: 100px;
  max-height: 100px;
}
a {
  display: block;
  float: left;
  border: 2px solid #eee;
  margin: 1px;
}
a img {
  border: 0;
}
a span {
  display: none;
  color: #fff;
}
a:hover span {
  display: block;
  position: fixed;
  background-color: #232323;
}
.title {
  top: 0;
  left: 0;
}
.price {
  top: 0;
  right: 0
}


あぁ初めて0から一人でつくった。とりあえずうれしい。

期間限定とか1年たったら出し直してくれたらいいのに。

次はなにしよう。