Created
October 27, 2014 12:36
-
-
Save beratdogan/312655e408b279b21b67 to your computer and use it in GitHub Desktop.
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
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