LoginSignup
9
9

More than 5 years have passed since last update.

みんな大好きIGRの時刻表をスクレイピング

Posted at

はじめに

皆さんこんにちは。
私は岩手県の大学に電車で通っています。
その電車こと、我ら学生がお世話になっている IGR!
いつもブラウザから時刻表を確認しています。
しかし、それが 見づらい(あくまで主観です) ので非常にめんどくさいです...。
そこで!この前スクレイピングして次の電車を出力するスクリプトを書きましたので紹介します。

青山駅の例

現在時刻から次の電車3つを出力するようにしてます。

scrape.rb
# URLにアクセスするためのライブラリの読み込み                                                                                                                 
require 'open-uri'                                                                                                                                            
# Nokogiriライブラリの読み込み                                                                                                                                
require 'nokogiri'                                                                                                                                            

# スクレイピング先のURL(青山駅の例)                                                                                                                                       
url = 'http://www.igr.jp/wp/timetable/aoyama'                                                                                                                 

charset = nil                                                                                                                                                 
html = open(url) do |f|                                                                                                                                       
    charset = f.charset # 文字種別を取得                                                                                                                      
    f.read # htmlを読み込んで変数htmlに渡す                                                                                                                   
end                                                                                                                                                           

# htmlをパース(解析)してオブジェクトを生成                                                                                                                    
docs = Nokogiri::HTML.parse(html, nil, charset)                                                                                                               

TIMES_TARGET_NUMBER = 4                                                                                                                                       

#対象ページの時刻表欄抽出                                                                                                                                     
lines = docs.xpath("//div[@class='post']/p[#{TIMES_TARGET_NUMBER}]").text                                                                                     
times = lines.to_s.strip.split(/[\r\n]+/)                                                                                                                     

time_and_sts = times.map do |time|                                                                                                                            
    time.split(/[\s]/) if time =~ /^[0-9]+:[0-9]+/                                                                                                            
end.compact                                                                                                                                                   

now_time = Time.now                                                                                                                                           

next_trains = time_and_sts.map do |time|                                                                                                                      
    time_str = time[0].split(/[:]/)                                                                                                                           
    if now_time.hour <= time_str[0].to_i                                                                                                                      
        time if now_time.hour < time_str[0].to_i || now_time.min <= time_str[1].to_i                                                                          
    end                                                                                                                                                       
end.compact                                                                                                                                                   
puts next_trains

21:35分にスクリプト実行した場合の出力結果はこんな感じに。

出力結果
$ ruby scrape.rb 
21:49
八戸
22:39
滝沢★
23:01
いわて沼宮内
23:24
滝沢★
※土休日運休

おわりに

このスクリプト使ってrailsでアプリケーション作ろうと思い途中でした笑
これでブラウザから確認しなくてもスクリプトから確認できますね!
IGRこれからもお世話になります!

9
9
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
9
9