Skip to content

Instantly share code, notes, and snippets.

@wagurano
Created December 10, 2013 02:12
Show Gist options
  • Save wagurano/7884747 to your computer and use it in GitHub Desktop.
Save wagurano/7884747 to your computer and use it in GitHub Desktop.
건강보험심사평가원 병원 코드 가져오기
#encoding: utf-8
require 'net/http'
require 'nokogiri'
require 'open-uri'
require 'timeout'
TIMEOUT_CNT = 42
n = 1 # 500 # 1614
begin
retries = TIMEOUT_CNT
begin
cnt = 0
code = ""
Timeout::timeout(5) {
doc = Nokogiri::HTML(open("http://m.hira.or.kr/eva/list.do?cateID=&p=#{n}"))
doc.xpath('//li/a').each do |a|
code = a['href'].scan(/code=(.*)/)
if !code.empty?
cnt = cnt + 1 # if retries >= TIMEOUT_CNT
code = code.join()
puts "#{n},#{cnt},#{code},'list',#{retries}"
end
end #doc.xpath
}
rescue Timeout::Error
retries -= 1
if retries > 0
puts "sleep"
sleep 0.42
retry
else
puts "raise"
raise
end
end
puts "#{n},#{cnt},CHECK_CODE,-,-"
n = n + 1
end while 0 < cnt
puts "#{n},#{cnt},0_LT_CNT,-,-"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment