roundTripFlightQuote(self, From, To)

Programming Language: Python 3 (As of Jan 2022)

Description


Travel() must be initialized first before calling roundTripFlightQuote(). However, as of currently, the location that gets initialized in the Travel() function will not be used for the roundTripFlightQuote() parameters. This function connects to Kayak.com in their round trip flights 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/flights/"+ From + "-" + To + ". This currently only gets the one best round trip flight for the desire 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.roundTripFlightQuote('CLT','ATL')
        
        
    

Class Code


        
            def roundTripFlightQuote(self, From, To):
              URL = "https://www.kayak.com/flights/"+ From + "-" + To + "/2022-02-22/2022-02-23?sort=bestflight_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")
              results_price = soup.find('span', class_='price option-text')
              results_description = soup.find("ol",class_="flights")
              time_pair = results_description.find_all('span',class_ = 'time-pair')
              print("Round-Trip Plane Trip for", From,'to',To)
              print ("Price:",results_price.text.strip())
              times = []
              for time in time_pair:
                times.append(time.text.replace("\n",""))
              print (From,'-',To,"Departure Time:",times[0],"-",times[1])
              print (To,'-',From,"Departure Time:", times[2],'-',times[3])
              print('\n')
        
    

Additional Information


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