From 489330a37b15a63f55b6b781dda8a7167e1bca00 Mon Sep 17 00:00:00 2001 From: Florian Schweikert Date: Fri, 2 Sep 2011 21:07:12 +0200 Subject: [PATCH] moved iParser to own file finished get lines method maybe fixed parseCorrection --- iTip.py | 33 +++++++++++++++++++++++++++++++++ parseCorrection.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 iTip.py create mode 100644 parseCorrection.py diff --git a/iTip.py b/iTip.py new file mode 100644 index 0000000..1b82e83 --- /dev/null +++ b/iTip.py @@ -0,0 +1,33 @@ +from BeautifulSoup import BeautifulSoup +import urllib2 +import settings + +class iParser: + + def __init__(self): + self._stations = {} + self._lines = [] + + def get_stations(self, letter): + if not self._stations.has_key(letter): + bs = BeautifulSoup(urllib2.urlopen(settings.stations % letter)) + self._stations[letter] = map(lambda x: x['value'], bs.find('select', {'id': 'letter'}).findAll('option')) + + return self._stations[letter] + + def get_lines(self): + if not self._lines: + bs = BeautifulSoup(urllib2.urlopen(settings.line_overview)) + # get tables + lines = bs.findAll('td', {'class': 'linie'}) + self._lines = [] + + for line in lines: + if line.a: + print line.text + if line.text: + self._lines.append((line.text, line.a['href'])) + elif line.img: + self._lines.append((line.img['alt'], line.a['href'])) + + return self._lines \ No newline at end of file diff --git a/parseCorrection.py b/parseCorrection.py new file mode 100644 index 0000000..be37f5d --- /dev/null +++ b/parseCorrection.py @@ -0,0 +1,31 @@ +from BeautifulSoup import BeautifulSoup + +class ParserError(Exception): + def __init__(self, value='', code=0): + self.value = value + self.code = code + + def __str__(self): + return repr(self.value) + +class Parser: + + def __init__(self, html): + self.soup = BeautifulSoup(html) + + def find_options(self): + nlo = self.soup.find('select', {'id': 'nameList_origin'}) + nld = self.soup.find('select', {'id': 'nameList_destination'}) + + if not nlo or not nld: + raise ParserError('Unable to parse html') + + origin = nlo.findAll('option') + destination = nld.findAll('option') + + if not origin: + origin = [] + if not destination: + destination = [] + + return (origin, destination) \ No newline at end of file -- 1.7.9.5