1

I've written a python script that takes flight data from Google Flights and writes it to a text document. When I run the code from terminal using python3 code.py, it works perfectly and I don't get this error. But, when I added the script to my entry point, it starts throwing the following error:

 Max retries exceeded with url: /session/2070f9827b90fc53d25392991e7b1855/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fef3d47d520>: Failed to establish a new connection: [Errno 61] Connection refused'))

The line in my code throwing this error is:

browser.get(url)

Oddly enough, the code works just fine, despite throwing this error. It still properly writes files and everything, so I'm not sure exactly what's happening.

Here's my full code:

from selenium import webdriver
from time import time, sleep
from datetime import datetime
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import os


#config headless mode
options = Options()
options.headless = True
options.add_argument('--window-size=1920,1200')

#Give path to chrome driver using service argument so it doesn't throw the path deprecation warning
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
chromedriver_path = '/path/to/chromedriver'
abs_chromedriver_path = os.path.join(script_dir, chromedriver_path)

driver_service = Service(executable_path = abs_chromedriver_path)
browser = webdriver.Chrome(options=options,service = driver_service)


#all variables
url = 'https://www.google.com/travel/flights'
clickList =     [
        "//input[@aria-label='Departure']",
        "//div[@data-iso='2022-09-06']//div[@role='button']",
        "//div[@data-iso='2022-10-16']//div[@role='button']",
        "//div[@class='WXaAwc']//button"
        ]

#Click function
def browserClick(xPath):
    browser.find_element(By.XPATH, xPath).click()
#Enter function
def browserEnter(xPath):
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, xPath))).send_keys(Keys.ENTER)
#Type function
def browserType(xPath,phrase):
    browserClick(xPath)
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, xPath))).send_keys(phrase)
    browserEnter(xPath)
#Average function
def getAverage(lst):
    return sum(lst) / len(lst)

def run():

    browser.get(url)

    browserType("//div[@aria-label='Enter your origin']//preceding-sibling::div[1]//input", "Barcelona")
    browserType("//div[@aria-label='Enter your destination']//preceding-sibling::div[1]//input", "JFK")

    for click in clickList:
        browserClick(click)
        sleep(0.5)

    sleep(10)

    mainList = browser.find_elements(By.XPATH, '//ul[@class="Rk10dc"]//li')

    mainList.pop()

    prices = []
    i = 1

    #Time stamp
    now = datetime.now()
    timeTit = str(now)
    dt_string = now.strftime('%d/%m/%Y %H:%M')
    priceTime = now.strftime('%H:%M')

    #Setup backup file
    #script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
    rel_path_backup = 'backups/BackupFile.txt'
    abs_backup_path = os.path.join(script_dir, rel_path_backup)
    backupFile = open(abs_backup_path,'a')
    backupFile.write('\nBelow data scraped at: ' + dt_string + '\n')


    #Scrape flight data, distribute to python lists
    for element in mainList: 
        #write whole item to backup data
        item = element.text
        backupFile.write('\n' + item + '\n')
        #parse item for price
        tempList = item.splitlines()
        #Check for stops
        if 'Nonstop' in tempList:
            priceSymInt = tempList[9]
        else:
            priceSymInt = tempList[10]
        priceInt = priceSymInt.replace('$','')
        if ',' in priceInt:
            priceInt = priceInt.replace(',','')
        prices.append(int(priceInt))
        i += 1

    #Close after all items have been processed
    backupFile.close()

    #Calculate average price
    avgPrice = getAverage(prices)

    rel_path_prices = 'prices/PricesFile.txt'
    abs_prices_path = os.path.join(script_dir, rel_path_prices)
    priceFile = open(abs_prices_path,'a')
    priceFile.write(priceTime + ',' + str(avgPrice) + '\n')
    priceFile.close()
    
    browser.close()
    browser.quit()

run()

Fox
  • 71
  • 4

0 Answers0