Skip to content

Instantly share code, notes, and snippets.

@noplans
Created September 9, 2009 01:05
Show Gist options
  • Save noplans/183379 to your computer and use it in GitHub Desktop.
Save noplans/183379 to your computer and use it in GitHub Desktop.
# encoding: utf-8
# Mac整備済み製品
require 'nokogiri'
require 'open-uri'
BASE = 'http://store.apple.com/'
TOP = '/jp/browse/home/specialdeals/mac'
def link(product)
anchor = product.at('td[@class="specs"]/h3/a')
{ :link => URI.join(BASE, anchor['href']).to_s,
:text => anchor.inner_html.gsub(/[\n\s]*(.*)[\n\s]*/){$1} }
end
def price(product)
product.at('span[@class="current_price"]').inner_html
end
def info(product)
{}.merge(link(product)).merge( {:price => price(product)} )
end
doc = Nokogiri::HTML(open(URI.join(BASE, TOP)).read)
products = doc.xpath('//tr[@class="product"]')
products = [].tap{|x| products.each{|product| x << info(product) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment