Created
September 9, 2009 01:05
-
-
Save noplans/183379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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