用硒刮擦。VS 代码未显示任何错误,但当程序运行时没有任何反应

Scraping with Selenium. VS code shows no errors but when program runs nothing happens

提问人:Yamar Lyons 提问时间:11/10/2023 最后编辑:Yamar Lyons 更新时间:11/10/2023 访问量:32

问:

我正在为学校做一个顶点项目,我遇到了一个问题,即程序运行但什么也没发生。我有正确的网络驱动程序,我的 python 是最新的。我真的很沮丧,所以任何帮助都会很棒。我将粘贴下面的代码。我还将放置一个图像,说明当我运行它时会发生什么,我总是必须强制结束程序。

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import TimeoutException
import time

import pandas as pd

import smtplib
from email.message import EmailMessage

import schedule

departure_flight_inputs={'Departure': " ORD",
               'Arrival': " LAS",
               'Date': "Jun 1, 2024"}

return_flight_inputs={'Departure': " LAS",
               'Arrival': " ORD",
               'Date': "Jun 8, 2024"}

def find_cheapest_flights(flight_info):
    PATH = R"C:\Users\theya\OneDrive\Desktop\FlightPriceTracker\chromedriver.exe"
    driver = webdriver.Chrome(executable_path=PATH)

    leaving_from = flight_info['Departure']
    going_to = flight_info['Arrival']
    trip_date = flight_info['Date']
    
    
    #Go to Expedia
    driver.get( "https://expedia.com")

   #Click on Flights
   # The flight_xpath represent the flight button on expedia
    flight_xpath = "//a[@class='uitk-tab-anchor uitk-tab-anchor-selected' and @aria-controls='search_form_product_selector_flights']"
    flight_element = WebDriverWait(driver,5).until(
        EC.presence_of_element_located((By.XPATH, flight_xpath))
        )
    flight_element.click()
    time.sleep(0.2)

      #Click on One-Way. IBecause it is easier this way.
    oneway_xpath = "//a[@class='uitk-tab-anchor uitk-tab-anchor-selected' and @aria-controls='FlightSearchForm_ONE_WAY']"
    one_way_element = WebDriverWait(driver,5).until(
        EC.presence_of_element_located((By.XPATH, oneway_xpath))
        )
    one_way_element.click()
    time.sleep(0.2)

   #Part 1: Flying From, Flying To, Departure Date, Return Date
    
    #**********************  Complete Leaving From Portion  **********************
    leaving_from_xpath = "//button[@aria-label='Leaving from' and @data-stid='origin_select-dialog-trigger']"
    leaving_from_element = WebDriverWait(driver,5).until(
        EC.presence_of_element_located((By.XPATH, leaving_from_xpath))
        )
    leaving_from_element.clear
    leaving_from_element.click()
    time.sleep(1)
    leaving_from_element.send_keys(leaving_from)
    
    time.sleep(1) #Need this otherwise it will be too fast for the broswer
    leaving_from_element.send_keys(Keys.DOWN, Keys.RETURN)
    #**********************  Complete Leaving From Portion  **********************
    
    
    
    #**********************  Complete Going To Portion  **********************
    going_to_xpath = "//button[@aria-label='Going to' and @data-stid='destination_select-dialog-trigger']"
    going_to_element = WebDriverWait(driver,5).until(
        EC.presence_of_element_located((By.XPATH, going_to_xpath))
        )
    going_to_element.clear
    going_to_element.click()
    time.sleep(1)
    going_to_element.send_keys(going_to)
    
    time.sleep(1) #Need this otherwise it will be too fast for the broswer
    going_to_element.send_keys(Keys.DOWN, Keys.RETURN) #Go down on item and click on it
    #**********************  Complete Going To Portion  **********************
    
    
    
    #**********************  Complete Departure Date Portion  **********************
    departing_box_xpath = '//button[contains(@aria-label,"Departing")]'
    
    depart_box_element = WebDriverWait(driver,5).until(
        EC.presence_of_element_located((By.XPATH, departing_box_xpath))
        )
    
    depart_box_element.click() #Click on the departure box
    time.sleep(2)
    
    
    #Find the current date. WILL arrow through too
    trip_date_xpath = '//button[contains(@aria-label,"{}")]'.format(trip_date)
    departing_date_element = ""
    while departing_date_element == "":
        try:
            departing_date_element = WebDriverWait(driver,3).until(
            EC.presence_of_element_located((By.XPATH, trip_date_xpath))
            )
            departing_date_element.click() #Click on the departure date
        except TimeoutException:
           departing_date_element=""
           next_month_xpath = "//button[@data-stid='uitk-calendar-navigation-controls-next-button'][2]"
           driver.find_element_by_xpath(next_month_xpath).click()
           time.sleep(1)
    
    depart_date_done_xpath = "//button[@data-stid='apply-date-selector' and text()='Done']"
    driver.find_element_by_xpath(depart_date_done_xpath).click()
    #**********************  Complete Departure Date Portion  **********************
    
    
    #**********************  Click Search  **********************
    search_button_xpath =  "//button[@id='search_button' and @type='submit' and @aria-label='Search' and text()='Search']"
    driver.find_element_by_xpath(search_button_xpath).click()
    time.sleep(15) #Need to let the page load properly
    #**********************  Click Search  **********************

     #Part 2: Setting Conditions for our flight
    
    #**********************  Check for Nonstop Flights Sorted by Lowest Price  **********************
    nonstop_flight_xpath = "//label[@for='NUM_OF_STOPS-0-qoo' and span[@class='uitk-checkbox-label-content']]"
    one_stop_flight_xpath = "//label[@for='NUM_OF_STOPS-1-8qb' and span[@class='uitk-checkbox-label-content')]]"
    
    
    if len(driver.find_elements_by_xpath(nonstop_flight_xpath)) > 0:
        
        driver.find_element_by_xpath(nonstop_flight_xpath).click()
        time.sleep(5)
        
        #Check if there are available flights
        available_flights = driver.find_elements_by_xpath("//span[contains(text(),'Select and show fare information ')]")
        if len(available_flights) >  0:
            if len(available_flights) == 1: #Don't have to sort by prices here
                flights = [(item.text.split(",")[0].split('for')[-1].title(),
                            item.text.split(",")[1].title().replace("At",":"),
                            item.text.split(",")[2].title().replace("At",":"),
                            item.text.split(",")[3].title().replace("At",":")) for item in available_flights[0:5]]

            else:
                #Sort by lowest prices
                driver.find_element_by_xpath( "//select[@id='sort-filter-dropdown-SORT']/option[@value='PRICE_INCREASING']").click()
                time.sleep(5)
                flights = [(item.text.split(",")[0].split('for')[-1].title(),
                            item.text.split(",")[1].title().replace("At",":"),
                            item.text.split(",")[2].title().replace("At",":"),
                            item.text.split(",")[3].title().replace("At",":")) for item in available_flights[0:5]]
            
            
            print("Conditions satisfied for: {}:{}, {}:{}, {}:{}".format("Departure",leaving_from,
                                                             "Arrival",going_to,
                                                             "Date",trip_date))
            driver.quit()
            return flights
    else:
        print('Not all conditions could be met for the following: "{}:{}, {}:{}, {}:{}'.format("Departure",leaving_from,
                                                                                             "Arrival",going_to,
                                                                                             "Date",trip_date))
        driver.quit()
        return []
    
    

    #**********************  Check for Nonstop Flights Sorted by Lowest Price  **********************



def send_email():
    #Get return values
    departing_flights = find_cheapest_flights(departure_flight_inputs)
    return_flights = find_cheapest_flights(return_flight_inputs)

    #Put it into a dataframe to visualize this more easily
    df = pd.DataFrame(departing_flights + return_flights)
    
    if not df.empty: #Only send an email if we have actual flight info
        email = open('[email protected]').read()
        password=open('$mallJoke11').read()
        
        msg = EmailMessage()
        
        msg['Subject'] = "Python Flight Info! {} --> {}, Departing: {}, Returning: {}".format(departure_flight_inputs['Departure'], departure_flight_inputs['Arrival'], departure_flight_inputs['Date'],return_flight_inputs['Date'])
        
        msg['From'] = email
        msg['To'] = email
        
        msg.add_alternative('''\
            <!DOCTYPE html>
            <html>
                <body>
                    {}
                </body>
            </html>'''.format(df.to_html()), subtype="html")
    
            
        with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
            smtp.login(email,password)
            smtp.send_message(msg)
    

schedule.clear()
schedule.every(30).minutes.do(send_email)

while True:
    schedule.run_pending()
    time.sleep(1)

我尝试更新我的 Chrome 和我正在使用的 Chromedriver,因为我认为也许这就是它不起作用的原因。我还检查了是否有任何不良缩进,但找不到任何缩进。

python selenium-webdriver selenium-chromedriver

评论

0赞 Olivier Samson 11/10/2023
尝试调试代码并逐步执行脚本。它将帮助您了解哪里出了问题。
0赞 John Gordon 11/10/2023
我怀疑它会在 30 分钟内运行您的函数,而不是现在。schedule.every(30).minutes
0赞 Yamar Lyons 11/10/2023
@JohnGordon非常感谢,多亏了你,我才能够解决这个问题。但是现在程序运行到打开出发信息的程度,但它没有在其中输入任何内容,只是超时。

答: 暂无答案