Skip to content

Instantly share code, notes, and snippets.

@mislav
Created December 1, 2009 14:25
Show Gist options
  • Save mislav/246309 to your computer and use it in GitHub Desktop.
Save mislav/246309 to your computer and use it in GitHub Desktop.
Solving Railscast #190 with Nibbler
#!/usr/bin/env ruby -rubygems
## solving Railscast #190 with Nibbler
## http://github.com/mislav/nibbler
## http://railscasts.com/episodes/190-screen-scraping-with-nokogiri
require 'nokogiri'
require 'open-uri'
require 'nibbler'
class Walmart < Nibbler
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