TrainBusQuotes(self, From, To)

Programming Language: Python 3 (As of Jan 2022)

Description


Travel() must be initialized first before calling trainBusQuotes(). However, as of currently, the location that gets initialized in the Travel() function will not be used for the trainBusQuotes() parameters. This function connects to Kayak.com in their train and bus sections of the website. The parameters are concatenated with the URL variable that is in the function. Example of URL variable: "https://www.kayak.com/trains/" + From + "-" + To + "/2022-03-20/2022-03-22?sort=duration_a". This currently only gets the one best train or bus option for the desired From and To locations.

Parameters


From, To

  Values of these must be a string of a correctly spelled location.

  From, To: string needs to be a city or IATA Code

Calling Example


        
            atlanta.trainBusQuotes('ATL','CAE')
        
        
    

Class Code


        
            def trainBusQuotes(self, From, To):
              URL = "https://www.kayak.com/trains/" + From + "-" + To + "/2022-03-20/2022-03-22?sort=duration_a"
              headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:76.0) Gecko/20100101 Firefox/76.0'}
              page = requests.get(URL, headers=headers)
              soup = BeautifulSoup(page.content, "html.parser")
              origin_station = soup.find('span', class_='top-row-station origin')
              destination_station = soup.find('span', class_='top-row-station destination')
              train_depart_times = soup.find('div', class_ = 'col-field time depart')
              train_return_times = soup.find('div', class_ = 'col-field time return')
              train_price = soup.find('span',class_ = 'price option-text')
              print("Train/Bus Trip for", From, 'to', To)
              print('Price:', train_price.text.replace('\n',''))
              print('Train Departure Time -',origin_station.text,'-',train_depart_times.text.replace('\n',''))
              print('Train Arrival Time -',destination_station.text,'-',train_return_times.text.replace('\n',''))
              print('\n')
        
    

Additional Information


Access to internet is needed to run this function when called.