LoginSignup
0
0

More than 3 years have passed since last update.

衆議院の質問主意書データをWebページから集計する

Last updated at Posted at 2020-03-19

国会に提出された質問主意書データをRubyを使って集計する。

集計に使った元データが必要な方は、@ts_3156までご連絡ください。データの各種加工もお引き受けいたします。

集計結果は以下の通り。

国会議員ごとのランキング

1位 初鹿 明博 20回
2位 櫻井 周 17回
3位 松原 仁 16回
4位 丸山 穂高 16回
5位 城井 崇 8回
6位 早稲田 夕季 8回
7位 下地 幹郎 6回
8位 中谷 一馬 5回
9位 阿部 知子 4回
10位 江田 憲司 3回
11位 山井 和則 3回
12位 森山 浩行 2回
13位 岡本 充功 2回
14位 大河原 雅子 2回
15位 柿沢 未途 1回
16位 山崎 誠 1回
17位 中島 克仁 1回
18位 山内 康一 1回
19位 稲富 修二 1回
20位 青山 大人 1回
21位 関 健一郎 1回
22位 赤嶺 政賢 1回
23位 黒岩 宇洋 1回
24位 亀井 亜紀子 1回
25位 大西 健介 1回
26位 階 猛 1回
27位 奥野 総一郎 1回

会派ごとのランキング

1位 立憲民主・国民・社保・無所属フォーラム 82回
2位 無所属 42回
3位 日本共産党 1回

以下、集計に使ったRubyコード

Gemfile
gem 'anemone'
require 'anemone'

def scrape(url)
  Anemone.crawl(url, depth_limit: 0) do |anemone|
    anemone.on_every_page do |page|
      return page
    end
  end
end

url = 'http://www.shugiin.go.jp/internet/itdb_shitsumon.nsf/html/shitsumon/kaiji201_l.htm'
progress_links = []
question_links = []
answer_links = []

scrape(url).links.each do |link|
  str = link.to_s

  if str.match?(/[^ab]201\d{3}\.htm/)
    progress_links << link
  elsif str.match?(/a201\d{3}\.htm/)
    question_links << link
  elsif str.match?(/b201\d{3}\.htm/)
    answer_links << link
  end
end

questions = []

progress_links.each do |link|
  page = scrape(link.to_s)
  html = Nokogiri::HTML(page.body)
  table = html.xpath('//*[@id="mainlayout"]/table')
  question = {}

  table.xpath('tr').each do |row|
    komoku = row.xpath("td[@headers='KOMOKU']").text
    naiyo = row.xpath("td[@headers='NAIYO']").text

    next if komoku == ''
    question[komoku] = naiyo
  end

  questions << question

  sleep 10
end

提出者名で順位を集計する場合

ranking = Hash.new(0)
questions.each do |question|
  key = question['提出者名']
  # key = question['会派名']
  ranking[key] += 1
end; nil

ranking.sort_by { |k, v| -v }.each.with_index do |(key, count), i|
  puts "#{i + 1}#{key.chomp('君')} #{count}回"
end

集計に使った元データが必要な方は、@ts_3156までご連絡ください。データの各種加工もお引き受けいたします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0