Skip to content

Instantly share code, notes, and snippets.

@stan
Forked from mislav/walmart-scraper.rb
Created January 29, 2010 17:38
Show Gist options
  • Save stan/289929 to your computer and use it in GitHub Desktop.
Save stan/289929 to your computer and use it in GitHub Desktop.
## solving Railscast #190 with "Scraper"
## http://github.com/mislav/scraper
## http://railscasts.com/episodes/190-screen-scraping-with-nokogiri
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'scraper'
class Walmart < Scraper
element :title
elements '.item' => :items do
element '.prodLink' => :title
element '.PriceCompare .BodyS, .PriceXLBold' => :price, :with => lambda { |node|
node.inner_text[/\$[0-9\.]+/]
}
element 'a/@href' => :href
end
end
result = Walmart.parse open('http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find')
puts result.title
result.items.each do |item|
puts "#{item.title} - #{item.price}\n #{item.href}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment