Last active
July 4, 2017 08:43
-
-
Save alea12/842d9cbe87f23ec6ba752de36d4bdad1 to your computer and use it in GitHub Desktop.
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
# 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