Skip to content

Instantly share code, notes, and snippets.

@beratdogan
Created October 27, 2014 12:36
Show Gist options
  • Save beratdogan/312655e408b279b21b67 to your computer and use it in GitHub Desktop.
Save beratdogan/312655e408b279b21b67 to your computer and use it in GitHub Desktop.
import requests
import lxml.html
from lxml.cssselect import CSSSelector
def get_filtered_nodes(content, selector):
dom_tree = lxml.html.fromstring(content)
selector = CSSSelector(selector)
return selector(dom_tree)
def get_routes_by_stop(url):
r = requests.get(url)
routes = get_filtered_nodes(r.text, ".table-search-result td:first-child a")
return [route.text for route in routes]
def find_bus_stop(input):
r = requests.get("http://mobil.iett.gov.tr/tr/mobil/durakarama/" + input)
bus_stops = get_filtered_nodes(r.text, ".table-search-result a")
data = {}
for bus_stop in bus_stops:
data[bus_stop.get("href")] = bus_stop.text
count = 0
for url, name in data.items():
print "[", count, "]", name
count += 1
choice = int(raw_input("Please enter the stop index you want: "))
return data.keys()[choice]
start = find_bus_stop(raw_input("Please enter start bus stop: "))
finish = find_bus_stop(raw_input("Please enter finish bus stop: "))
start_buses = set(get_routes_by_stop(start))
finished_buses = set(get_routes_by_stop(finish))
buses = filter(None, start_buses & finished_buses)
print "You can use these buses: ", ", ".join(buses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment