Created
December 1, 2009 14:25
-
-
Save mislav/246309 to your computer and use it in GitHub Desktop.
Solving Railscast #190 with Nibbler
This file contains hidden or 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
#!/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