Last active
August 29, 2015 13:58
-
-
Save tomo3141592653/10336890 to your computer and use it in GitHub Desktop.
本の名前を入力するとamazonのurlが表示されるrubyスクリプト
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
# -*- 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