Created
June 10, 2016 13:10
-
-
Save mtsuszycki/97460b726970da3090a0ef0694f5b051 to your computer and use it in GitHub Desktop.
Get product price from amazon website, ruby, mechanize
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
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'mechanize' | |
# to avoid 'ran out of buffer space on element' error | |
module WWW | |
require 'hpricot' | |
class Mechanize | |
Hpricot.buffer_size = 262144 # added by naofumi | |
end | |
end | |
$agent = "Linux Mozilla" | |
$site = "http://amazon.co.uk" | |
def get_price(url) | |
agent = WWW::Mechanize.new | |
agent.user_agent_alias = $agent | |
page = agent.get($site + "/" + url) | |
title = '' | |
# uncomment if you want product title as well | |
# title = '#' + page.search("//[@id='productTitle']").inner_html.strip | |
return page.search("//[@id='priceblock_saleprice']").inner_html[1,10] + " #{title}" | |
end | |
unless ARGV[0] | |
puts "Need one arg: product_name/dp/ASIN_number (copied from amazon product url)" | |
puts "example: amazon_get_price.rb Massage-Stress-Reflexology-Porcupine-Sensory/dp/B00M776F12" | |
exit 0 | |
end | |
puts get_price(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment