Skip to content

Instantly share code, notes, and snippets.

@alea12
Last active July 4, 2017 08:43
Show Gist options
  • Save alea12/842d9cbe87f23ec6ba752de36d4bdad1 to your computer and use it in GitHub Desktop.
Save alea12/842d9cbe87f23ec6ba752de36d4bdad1 to your computer and use it in GitHub Desktop.
# generates seeds.rb from tabelog area definitions
#
# Usage:
# $ ruby tabelog.rb >> db/seeds.rb
#
# Premises:
# $ rails g model Prefecture name
# $ rails g model Area prefecture:references name
require 'nokogiri'
require 'open-uri'
BASE_URL = 'https://tabelog.com'
TARGET_PATH = '/sitemap'
pref_list = []
doc = Nokogiri::HTML.parse(open(BASE_URL + TARGET_PATH))
doc.css('div#arealst_sitemap a').each do |link|
pref_list << {name: link.children.text, url: link.attributes['href'].value}
end
pref_list.each do |pref|
puts "@p = Prefecture.create! name: '#{pref[:name]}'"
doc = Nokogiri::HTML.parse(open(BASE_URL + pref[:url]))
doc.css('div#arealst_sitemap a').each do |area|
puts "Area.create! prefecture: @p, name: '#{area.children.text}'"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment