Skip to content

Instantly share code, notes, and snippets.

@yoonchulkoh
Created May 14, 2017 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoonchulkoh/139a21637a1e76b80c6377719ca05a80 to your computer and use it in GitHub Desktop.
Save yoonchulkoh/139a21637a1e76b80c6377719ca05a80 to your computer and use it in GitHub Desktop.
iTS健康診断ページから空き情報一覧を取得する
require 'open-uri'
require 'nokogiri'
require 'mechanize'
namespace :scrape do
desc '健康診断ページから空き情報一覧を取得する'
task :medical_checkup => :environment do
@courses = {
# 1 => "1日人間ドック(午前)",
2 => "健保指定ドック(午前)",
# 4 => "1日人間ドック(午後)",
5 => "健保指定ドック(午後)",
}
def scrape_yoyaku(index, is_gynecology=false)
agent = Mechanize.new
base_url = 'http://www.its-kenpo.or.jp/kanri/chokuei/yoyaku/privacy_yoyaku_kojinyoyku.html'
page = agent.get(base_url)
page = page.link_with(:text => '同意する').click
# p agent.page.title
# 検索
page.form_with(:id => 'SearchSpaceIndexForm') do |f|
f.radiobutton_with(:id => 'SearchSpaceCondExaminationYear2017').check
f.radiobutton_with(:id => "SearchSpaceCondCourseSingle#{index}").check
if is_gynecology
f.checkbox_with(:id => "SearchSpaceCondOption1").check
f.checkbox_with(:id => "SearchSpaceCondOption3").check
end
f.click_button
end
gynecology = is_gynecology ? " (婦人科検査)" : ""
p "#{@courses[index]}#{gynecology}"
p "dates, place, count"
elements = agent.page.search('td a')
elements.each do |e|
text = e.get_attribute(:onclick) # => selectDate(1, '2018-01-05')
text = text.gsub(/selectDate\(/, '')
text = text.gsub(/\)/, '')
texts = text.split(', ')
place = texts[0] == "1" ? '大久保健診センター' : '山王健診センター'
date = texts[1]
count = e.inner_text
p "#{date}, #{place}, #{count}"
end
end
@courses.keys.each do |i|
scrape_yoyaku(i)
p ""
end
@courses.keys.each do |i|
scrape_yoyaku(i, true)
p ""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment