Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. from urllib.request import urlopen
  4. from requests.utils import quote
  5.  
  6. def suodataOsoite(osoite):
  7.         suodatettu=""
  8.         loytyi=False
  9.         p=int(0)
  10.         while p < len(osoite):
  11.             if osoite[p] not in "0123456789":
  12.                 loytyi=True
  13.                 suodatettu=suodatettu+osoite[p]
  14.             else:
  15.                 if loytyi==True:
  16.                     break    
  17.             p=p+1
  18.         loytyi=False
  19.         while p < len(osoite):
  20.             if osoite[p] in "0123456789":
  21.                 loytyi=True
  22.                 suodatettu=suodatettu+osoite[p]
  23.             else:
  24.                 if loytyi==True:
  25.                     break    
  26.             p=p+1
  27.         return suodatettu.strip()
  28.            
  29. def postiNroToimipaikka(osoite="helsinginkatu 1",kunta="helsinki",lista=[]):
  30.     if len(lista)==1:
  31.         return lista[0][3]+" "+lista[0][4]
  32.     else:
  33.         osnro=""
  34.         loytyi=False
  35.         for p in range(len(osoite)):
  36.             if osoite[p] in "0123456789":
  37.                 loytyi=True
  38.                 osnro=osnro+osoite[p]
  39.             else:
  40.                 if loytyi==True:
  41.                     break
  42.         if len(osnro)==0:
  43.             return "Useampi kuin yksi vaihtoehto"
  44.         for p in range(len(lista)):
  45.             if int(osnro)==int(lista[p][1]):
  46.                 return lista[p][3]+" "+lista[p][4]
  47.         for p in range(len(lista)):
  48.             if int(osnro)>int(lista[p][1]):
  49.                 if len(lista[p][2])>0:
  50.                     if int(osnro)<=int(lista[p][2]):
  51.                         return lista[p][3]+" "+lista[p][4]  
  52.     return "Tuntematon osoite"
  53.      
  54. def haePostinumero(osoite="helsinginkatu 1",kunta="helsinki"):
  55.  
  56.     url="http://www.verkkoposti.com/e3/"\
  57.          +"postinumeroluettelo?streetname="\
  58.          +quote(osoite.encode('ISO-8859-1'))\
  59.          +"&postcodeorcommune="\
  60.          +quote(kunta.encode('ISO-8859-1'))
  61.  
  62.     html=urlopen(url).read().decode("ISO-8859-1")
  63.  
  64.     lista=[]
  65.  
  66.     if html.find("Postinumerohaun tulos") > 0:
  67.         soup=BeautifulSoup(html,"lxml")
  68.         table = soup.find(class_="hidden-xs")
  69.         kadut=table.find_all("td")
  70.         for katu in range(0,len(kadut),4):
  71.             rivi=[]
  72.             katunimi=""
  73.             katunro1=""
  74.             katunro2=""
  75.             postinro=""
  76.             toimipaikka=""
  77.             kunta=""
  78.             katun=kadut[katu].find_all("div")
  79.             if len(katun) > 0:
  80.                 if len(katun) > 1:
  81.                     katul=katun[1].string.strip().split(' ')
  82.                 else:
  83.                     katul=katun[0].string.strip().split(' ')
  84.                 if len(katul) == 1:
  85.                     katunimi=katul[0].strip()
  86.                 if len(katul) == 2 or len(katul) == 4:
  87.                     katunimi=katul[0].strip()
  88.                     katunro1=katul[1].strip()
  89.                 if len(katul) == 4:
  90.                     katunro2=katul[3].strip()
  91.             toimip=kadut[katu+2].string.strip().split('\n')
  92.             postinro=toimip[0].strip()
  93.             toimipaikka=toimip[2].strip()
  94.             kunta=kadut[katu+3].string.strip()
  95.        
  96.             rivi.append(katunimi)
  97.             rivi.append(katunro1)
  98.             rivi.append(katunro2)
  99.             rivi.append(postinro)
  100.             rivi.append(toimipaikka)
  101.             rivi.append(kunta)
  102.             lista.append(rivi)
  103.  
  104.     return lista
  105.  
  106. def main():
  107.     while True:        
  108.         postinumerot=[]
  109.         kyskatu=input("Katuosoite: ")
  110.         kyskunta=input("Kunta: ")
  111.         kyskatu=suodataOsoite(kyskatu)
  112.         if len(kyskatu)>0 and len(kyskunta)>0:
  113.             postinumerot=haePostinumero(kyskatu,kyskunta)
  114.             print(postiNroToimipaikka(kyskatu,kyskunta,postinumerot))
  115.         elif len(kyskatu)==0 and len(kyskunta)>0:
  116.             break
  117.         elif len(kyskatu)>0 and len(kyskunta)==0:
  118.             postinumerot=haePostinumero(osoite=kyskatu)
  119.             print(postiNroToimipaikka(osoite=kyskatu,lista=postinumerot))
  120.         else:
  121.             postinumerot=haePostinumero()
  122.             print(postiNroToimipaikka(lista=postinumerot))
  123.  
  124. #        for postinumero in postinumerot:
  125. #            print(postinumero)
  126.  
  127.  
  128. if __name__ == "__main__":
  129.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement