Advertisement
jarekmor

PKP_info_2

Nov 18th, 2021
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import pandas as pd
  4. import re
  5.  
  6. url="https://infopasazer.intercity.pl/?p=station&id=33605"
  7.  
  8. html_content = requests.get(url)
  9. html_content.encoding = "utf-8"
  10.  
  11. soup = BeautifulSoup(html_content.text, "lxml")
  12.  
  13. gdp = soup.find_all("table", attrs={"class": "table table-delay mbn"})
  14. print("Number of tables on site: ",len(gdp))
  15.  
  16. table1 = gdp[0]
  17. body = table1.find_all("tr")
  18. head = body[0]
  19. body_rows = body[1:]
  20. headings = ['Numer', 'Przewoznik', 'Data', 'Relacja', 'Przyjazd_plan', 'Opoznienie']
  21.  
  22. all_rows = []
  23. for row_num in range(len(body_rows)):
  24.     row = []
  25.     for row_item in body_rows[row_num].find_all("td"):
  26.         aa = re.sub("(\xa0)|(\n)|,","",row_item.text)
  27.         row.append(aa)
  28.     all_rows.append(row)
  29.  
  30. df = pd.DataFrame(data=all_rows,columns=headings)
  31.  
  32. #print all connections from 'Warszawa Centralna' from current time
  33. print(df)
  34.  
  35. #dataframe for given connection i.e. "Kraków Główny - Gdynia Główna" from 'Warszawa Centralna'
  36. con = 'Kraków Główny - Gdynia'
  37. print()
  38. print('Departure time for connection {} from Warszawa-Centralna'.format(con))
  39. print(df[ df['Relacja'] == 'Kraków Główny - Gdynia Główna' ])
  40.  
  41.  
  42. OUTOUT:
  43. Number of tables on site:  2
  44.                 Numer          Przewoznik        Data                            Relacja Przyjazd_plan Opoznienie
  45. 0            93150/1   Koleje Mazowieckie  2021-11-19        Modlin - Warszawa Centralna         01:00      0 min
  46. 1     35170/1 KARPATY       PKP Intercity  2021-11-19      Kraków Główny - Gdynia Główna         05:23      0 min
  47. 2  61171/0 KARKONOSZE       PKP Intercity  2021-11-19  Jelenia Góra - Warszawa Wschodnia         05:45      0 min
  48.  
  49. Departure time for connection Kraków Główny - Gdynia from Warszawa-Centralna
  50.              Numer     Przewoznik        Data                        Relacja Przyjazd_plan Opoznienie
  51. 1  35170/1 KARPATY  PKP Intercity  2021-11-19  Kraków Główny - Gdynia Główna         05:23      0 min
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement