Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 20th, 2012  |  syntax: None  |  size: 3.03 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import urllib
  2. import urllib2
  3. from bs4 import BeautifulSoup
  4. import re
  5. import time
  6.  
  7. #---[ Functions ]---#
  8.  
  9. def isTimeFormat(input):
  10.     try:
  11.         time.strptime(input, '%H:%M')
  12.         return True
  13.     except ValueError:
  14.         return False
  15.  
  16.  
  17. #expressao regular pra remover a sujeira do campo da linha de onibus
  18. linha_r = re.compile('[\t\n\r\-\|]+')
  19.  
  20. url = "http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibus"
  21.  
  22. f = urllib2.urlopen(url)
  23. doc = BeautifulSoup(f.read())
  24. # print doc
  25. EMPRESAS = []
  26. for e in doc.select('select[name="empresa"] > option'):
  27.   if e.has_key("title"):
  28.         EMPRESAS.append((e["title"].strip().encode("utf-8"), int(e["value"])))
  29.  
  30. empresaId = 0
  31.  
  32. horarioIdIda = 0
  33. horarioIdVolta = 0
  34. itinaratioId = 0
  35.  
  36. for empresa,id in EMPRESAS:
  37.        
  38.         print 'EMPRESA :' + empresa
  39.         empresaId = empresaId +1
  40.  
  41.         #Cria Empresa no banco
  42.         empresadb = {
  43.                 'empresaNome': empresa,
  44.                 'empresaId': empresaId
  45.         }
  46.  
  47.         #Salvando no DB
  48.         print 'Empresa salva'
  49.         db.empresas.save(empresadb)
  50.  
  51.         data = {
  52.                 "empresa":id,
  53.                 "opcao":1,
  54.                 "passoGeral":1,
  55.                 "passoEmpresa":1,
  56.                 }
  57.         req = urllib2.Request(url, urllib.urlencode(data))
  58.         doc = BeautifulSoup(urllib2.urlopen(req).read())
  59.         for linha in doc.select('ul[class="listagem"] > li'):
  60.                 linha_data = linha_r.split(linha.contents[0].strip().encode("utf-8"))
  61.                 linha_numero = linha_data[0].strip()
  62.                 linha_nome = linha_data[1].strip()
  63.                 print '---> ONIBUS: ',linha_numero,"-",linha_nome.title()
  64.  
  65.                 #Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
  66.                 aba = {'passoGeral' : '3'}
  67.                 url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
  68.  
  69.                 data = urllib.urlencode(aba)    # Use urllib to encode the parameters
  70.                 request = urllib2.Request(url2, data)
  71.                 f = urllib2.urlopen(request)    # This request is sent in HTTP POST
  72.                 doc = BeautifulSoup(f.read())
  73.  
  74.                 x = doc.find_all('ul','listagem')[0]
  75.  
  76.                 for k in x.find_all('li'):
  77.                         print str(k)[16:65].title(),
  78.                                        
  79.  
  80.                 # Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
  81.                 aba = {'passoGeral' : '1'}
  82.                 url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
  83.  
  84.                 data = urllib.urlencode(aba)    # Use urllib to encode the parameters
  85.                 request = urllib2.Request(url2, data)
  86.                 f = urllib2.urlopen(request)    # This request is sent in HTTP POST
  87.                 doc = BeautifulSoup(f.read())
  88.  
  89.                 x = str(doc.find_all(valign="top")[1])
  90.  
  91.                 y=0
  92.                 while True:
  93.                         y = x.find('<br/>',y+1)
  94.                         if y == -1:
  95.                                 break
  96.                         else:
  97.                                 h = x[y-5:y]
  98.                                 if isTimeFormat(h) is True:
  99.                                         print h
  100.  
  101.                 #Passo geral 1 = IDA, 2 = VOLTA, 3 = ITINERARIO
  102.                 aba = {'passoGeral' : '2'}
  103.                 url2 = 'http://www.pmf.sc.gov.br/servicos/index.php?pagina=onibuslinha&idLinha='+linha_numero
  104.  
  105.                 data = urllib.urlencode(aba)    # Use urllib to encode the parameters
  106.                 request = urllib2.Request(url2, data)
  107.                 f = urllib2.urlopen(request)    # This request is sent in HTTP POST
  108.                 doc = BeautifulSoup(f.read())
  109.  
  110.                 x = str(doc.find_all(valign="top")[1])
  111.  
  112.                 print '------> Horario VOLTA:'
  113.                 y=0
  114.                 while True:
  115.                         y = x.find('<br/>',y+1)
  116.                         if y == -1:
  117.                                 break
  118.                         else:
  119.                                 h = x[y-5:y]
  120.                                 if isTimeFormat(h) is True:
  121.                                         print h