-
-
Save crossphd/88d1cb8bc5b197349d67e6080793b685 to your computer and use it in GitHub Desktop.
Ruby Amazon Scraper
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
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'colored' | |
# this is just a preview of what's to come - a proof of concept. | |
# it will be converted to a api-type library, gemified, and put in it's own repo | |
# for now, a cool way to experiment with amazon's data | |
query = 'ruby' | |
page = '2' | |
doc = Nokogiri::HTML(open("http://www.amazon.com/s/field-keywords=#{query}?page=#{page}")) | |
puts "Amazon search for '#{query}', page ##{page}\n".red.underline | |
doc.css('div.product').each do |el| | |
# grab the title | |
title = el.css('a.title').first.content | |
# grab the author (can be linked or not, hence the logic) | |
author = el.css('.ptBrand a').empty? ? el.css('.ptBrand').first.content.gsub!(/by /, '') : el.css('.ptBrand a').first.content | |
# grab the image | |
image = el.css('.productImage').attribute 'src' | |
# grab the product link | |
link = el.css('a.title').attribute 'href' | |
puts "#{title} by #{author}".green | |
puts "image url:".yellow + " #{image}" | |
puts "amazon link:".yellow + " #{link}" | |
puts "" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment