Skip to content

Instantly share code, notes, and snippets.

@siawyoung
Created April 23, 2015 13:54
Show Gist options
  • Save siawyoung/a5de0cd53d4887a0b453 to your computer and use it in GitHub Desktop.
Save siawyoung/a5de0cd53d4887a0b453 to your computer and use it in GitHub Desktop.
Ruby script for scraping bus stops -> bus numbers from SBS
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