Skip to content

Instantly share code, notes, and snippets.

@ohta-rh
Created July 27, 2013 04:58
Show Gist options
  • Save ohta-rh/6093767 to your computer and use it in GitHub Desktop.
Save ohta-rh/6093767 to your computer and use it in GitHub Desktop.
require "csv"
require "uri"
class CreateScrapingCsv
def make(search_keys, url, file_name)
csv_body = []
search_keys.each do |key|
csv_body << search(key,url)
end
make_csv(file_name, csv_body)
end
def search(key, url)
key_word = key.encode("UTF-8")
query = "/search?q=#{key_word}&ie=UTF-8&oe=UTF-8"
#検索キーワードでコール回数を調べる
res = Net::HTTP.start(url.host, url.port) {|http|
http.get(query)
}
[key ,(res.body =~ /<title>(.+)<\/title>/).to_s]
end
def make_csv(file_name ,body)
return unless body
CSV.open("./#{file_name}.csv","w") do |csv|
body.each {|b| csv << b}
end
end
end
#引数解析
url = URI.parse(ARGV.first)
host = url.host.to_s.split(".")
search_keys = ARGV[1].split(",")
result = CreateScrapingCsv.new
result.make(search_keys, url, host[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment