-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_judges.rb
executable file
·66 lines (58 loc) · 1.69 KB
/
get_judges.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
require 'open-uri'
require 'iconv'
require 'nokogiri'
require 'json'
# require 'charlock_holmes'
def write_json(filename, content)
File.open(filename,"w") do |f|
f.write(JSON.pretty_generate(content))
end
end
def get_html(url)
ic = Iconv.new('UTF-8//IGNORE', 'Big5')
page = open(url)
html = Nokogiri::HTML(ic.iconv(page.read))
end
url = 'http://csdi.judicial.gov.tw/abbs/wkw/WHD3A00.jsp'
query_url = 'http://csdi.judicial.gov.tw/abbs/wkw/WHD3A04.jsp?court='
html = get_html(url)
content = "單位,庭別,股別,法官\n"
courts = []
courts_section = html.css('option')
courts_section.each do |c|
court = {}
court[:name] = c.text
court[:code] = c.attr('value')
court[:divisions] = []
url = query_url + court[:code]
html = get_html(url)
tr_section = html.css('tr')
division = nil
tr_section.each do |tr|
if tr.attr('class') == 'title'
division = {}
division[:name] = tr.text.strip
division[:branchs] = []
court[:divisions] << division
elsif tr.attr('class') == 'tr-1'
td_section = tr.css('td')
if td_section[1].text.include? "法官"
branch = {}
branch[:branch] = td_section[0].text.strip
branch[:judge] = td_section[1].text.strip.split(' ')[0]
branch[:clerk] = td_section[2].text.strip
content += "#{court[:name]},#{division[:name]},#{branch[:branch]},#{branch[:judge]}\n"
puts "#{court[:name]},#{division[:name]},#{branch[:branch]},#{branch[:judge]}\n"
if division[:branchs]
division[:branchs] << branch
end
end
end
end
courts << court
end
write_json('data/judges.json', courts)
File.open("data/judges.csv","w") do |f|
f.write(content)
end