Created
April 23, 2015 13:54
-
-
Save siawyoung/a5de0cd53d4887a0b453 to your computer and use it in GitHub Desktop.
Ruby script for scraping bus stops -> bus numbers from SBS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'httparty' | |
all_bus_stops = [] | |
raw_doc = HTTParty.post('http://www.transitlink.com.sg/eservice/eguide/bscode_idx.php', :body => {"bs_code" => "01012"}) | |
doc = Nokogiri::HTML(raw_doc) | |
# we get all of the bus stops first | |
doc.css('option').each do |bus_stop| | |
all_bus_stops << bus_stop.content | |
end | |
# then for each bus stop, we issue a separate request | |
all_bus_stops.each do |bus_stop| | |
puts bus_stop | |
printy_string = bus_stop | |
raw_doc = HTTParty.post('http://www.transitlink.com.sg/eservice/eguide/bscode_idx.php', :body => {"bs_code" => bus_stop}) | |
doc = Nokogiri::HTML(raw_doc) | |
# find all HTML tags with svcNum and push it into the string | |
doc.css('.svcNum').each do |bus_number| | |
printy_string << " #{bus_number.content}" | |
end | |
# append the line into the file | |
open('bus_stops.txt', 'a') { |f| | |
f.puts printy_string | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment