Advertisement
Guest User

Parser

a guest
May 22nd, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. import urllib2
  2. import StringIO
  3. import lxml.html
  4. import sqlite3
  5.  
  6. c = sqlite3.connect('busy2.0.db')
  7. c.execute('''CREATE TABLE busy (numer text, kolejnosc text, przystanek text, kierunek text, id_trasy text)''')
  8. c.execute('''CREATE TABLE rozklad (numer text, typ_rozkladu text, id_przyst text, kierunek text, id_trasy text, godzina text)''')
  9. def fetch(url):
  10.     Query = {}
  11.     try:
  12.         Query = urllib2.urlopen(url)
  13.     except (urllib2.HTTPError,urllib2.URLError):
  14.         Query = fetch(url)
  15.     return Query
  16.  
  17. def pobierz_rozkl(nr_przyst, nr_trasy, nr_autobusa, nr_p, kier):
  18.     rozklad = fetch("http://rozklady.kzkgop.pl/wydruk.php?plik=przystankowo"+str(nr_przyst)+"_"+str(nr_trasy)+".php&numer_linii="+str(nr_autobusa)).read()
  19.     root = lxml.html.fromstring(rozklad)
  20.     i = root.cssselect("div#rozklad_tabliczka")[0]
  21.     surowo = lxml.html.tostring(i)
  22.     godziny = lxml.html.fromstring(surowo)
  23.     for g in godziny.cssselect('tr'):
  24.         czas = ''
  25.         try:
  26.             typ_rozkladu = g.cssselect('th')[0]
  27.             typ = typ_rozkladu.text
  28.         except IndexError:
  29.             w =0
  30.         for a in g.cssselect('span#blok_godzina'):
  31.             godz = a.cssselect('b')[0]
  32.             aktualna =  godz.text
  33.             for min in a.cssselect('sup'):
  34.                 czas += str(aktualna)+':'+str(min.text)+' '
  35.         ao = str(aktualna)+':'+str(min.text)
  36.         c.execute("Insert Into rozklad VALUES ('"+ str(nr_autobusa)+"','"+unicode(typ)+"','"+str(nr_p)+"','"+str(unicode(kier))+"','"+str(nr_trasy)+"','"+str(ao)+"')");
  37.         #print czas
  38.     #print root.cssselect("div#tabliczka_topinfo h2 a")[0].attrib['href']
  39.  
  40. #pobierz_rozkl(1,7562,'_T')
  41. def pobierz_autobusy():
  42.     strona = rozklad = urllib2.urlopen("http://rozklady.kzkgop.pl/index.php?co=rozklady").read()
  43.     root = lxml.html.fromstring(rozklad)
  44.     data = root.cssselect('div.zbior_linii')
  45.    
  46.     licznik =0
  47.     i = 0
  48.     for bus_all in data:
  49.         busy = bus_all.cssselect('span')
  50.         licznik +=1
  51.     print 'Wszystki wykrytych: : '+str(len(busy))
  52.         for sigle in busy:
  53.             i=i+1
  54.             if licznik == 1:
  55.                 c_nazwa = 'T'+str(sigle.text)
  56.             else:
  57.                 c_nazwa = sigle.text
  58.             print str(i)+". "+ c_nazwa
  59.             pobierz_przyst(c_nazwa, None)
  60.            
  61. def pobierz_przyst(nr_busa, data):
  62.     if data == None:
  63.         strona = fetch("http://rozklady.kzkgop.pl/index.php?co=rozklady&submenu=trasy&nr_linii="+str(nr_busa)).read()
  64.     else:
  65.         strona = fetch("http://rozklady.kzkgop.pl/index.php?co=rozklady&submenu=trasy&nr_linii="+str(nr_busa)+"&data="+str(data)).read()
  66.     try:
  67.         root = lxml.html.fromstring(strona)
  68.         count = 1
  69.     a = root.cssselect('div#div_tabelki_tras')
  70.         for o in a:
  71.             count = 1
  72.             kier =o.cssselect('div')[1]
  73.             if kier.attrib['id']=='lewo' or kier.attrib['id']!='prawo':
  74.         count = 2
  75.         for e in range(1,count+1):
  76.         kier =o.cssselect('div')[e]
  77.         for id in kier.cssselect('td.td_przystanek'):
  78.             #print id[0].text+' '+unicode(kier.attrib['id'])
  79.             href = id.cssselect('a')[0].attrib['href']
  80.             tt = href.split('&')
  81.             tableb = tt[3].split('=')
  82.             tablea = tt[4].split('=')
  83.             id_trasy = tablea[1]
  84.             nr_przyst = tableb[1]
  85.             c.execute("Insert Into busy VALUES ('"+str(nr_busa)+"','"+str(nr_przyst)+"','"+id[0].text+"','"+str(unicode(kier.attrib['id']))+"','"+str(id_trasy)+"')")
  86.             pobierz_rozkl(nr_przyst, id_trasy, nr_busa, nr_przyst, str(unicode(kier.attrib['id'])))
  87.         c.commit()
  88.     except IndexError:
  89.          root = lxml.html.fromstring(strona)
  90.          s = root.cssselect('div#div_tabelki_tras')[0]
  91.          rozklad = s.cssselect('li a b')[0]
  92.          pobierz_przyst(nr_busa,rozklad.text)
  93.          
  94. pobierz_autobusy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement