Skip to content

Instantly share code, notes, and snippets.

@tomo3141592653
Last active August 29, 2015 13:58
Show Gist options
  • Save tomo3141592653/10336890 to your computer and use it in GitHub Desktop.
Save tomo3141592653/10336890 to your computer and use it in GitHub Desktop.
本の名前を入力するとamazonのurlが表示されるrubyスクリプト
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'mechanize' #gem install mechanize
require 'uri'
def item2url(item)
item = item.split("(").first
item = item.split("/").first
item_escape = URI.escape(item)
url="http://www.amazon.co.jp/s/field-keywords=#{item_escape}"
agent = Mechanize.new
agent.user_agent_alias = 'Windows IE 7'
page = agent.get(url)
url_sell = page.search("h3.newaps").css("a")[0][:href]
id = url_sell.split("/").select{|x|/^\d+$/.match(x)}.first
return "http://www.amazon.co.jp/dp/#{id}/?tag=hougi-22"
end
while gets
item = $_.chomp
begin
puts item2url(item)
rescue
puts "cannot find #{item}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment