gpt4 book ai didi

python - 如何查看当前日期并转到下一个日期

转载 作者:行者123 更新时间:2023-11-28 18:32:50 25 4
gpt4 key购买 nike

我遇到了一个我似乎无法理解的 python 问题。不确定我是否需要使用 if 语句,但因为我是 python 的新手,所以我实际上不确定如何编写这个小问题。

实际上,这就是我遇到的问题。对于出发日历,我希望 python 能够执行以下操作:

  • 查看“您的约会对象”。如果有航类(不管是低价还是正常),点击它。如果没有,则转到下一个有航类的可用日期并单击该日期。
  • 如果当前月份没有可用日期,则需要能够移至下个月(我有一个示例代码)。

对于返程日历,我希望它做同样的事情,但要确保它选择的日期至少比所选出发日期晚 7 天。

这实际上是我的问题,如何做到这一点?

下面是出发日历的html(回程日历完全一样,只是它是inboundsearchresults而不是outbound search results):

下面我有一个示例代码,如果您想使用该模板并对其进行操作,它可以在从普通日期选择器(在 url 之前的页面中使用)中进行选择时使用:

# select depart date
datepicker = driver.find_element_by_id("departure-date-selector")
actions.move_to_element(datepicker).click().perform()

# find the calendar, month and year picker and the current date
calendar = driver.find_element_by_id("departureDateContainer")
month_picker = Select(calendar.find_element_by_class_name("ui-datepicker-month"))
year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
current_date = calendar.find_element_by_class_name("ui-datepicker-current-day")

# printing out current date
month = month_picker.first_selected_option.text
year = year_picker.first_selected_option.text
print("Current departure date: {day} {month} {year}".format(day=current_date.text, month=month, year=year))

# see if we have an available date in this month
try:
next_available_date = current_date.find_element_by_xpath("following::td[@data-handler='selectDay' and ancestor::div/@id='departureDateContainer']")
print("Found an available departure date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
except NoSuchElementException:
# looping over until the next available date found
while True:
# click next, if not found, select the next year
try:
calendar.find_element_by_class_name("ui-datepicker-next").click()
except NoSuchElementException:
# select next year
year = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
year.select_by_visible_text(str(int(year.first_selected_option.text) + 1))

# reporting current processed month and year
month = Select(calendar.find_element_by_class_name("ui-datepicker-month")).first_selected_option.text
year = Select(calendar.find_element_by_class_name("ui-datepicker-year")).first_selected_option.text
print("Processing {month} {year}".format(month=month, year=year))

try:
next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']")
print("Found an available departure date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
break
except NoSuchElementException:
continue

最佳答案

想法是定义一个可重复使用的函数 - 调用它 select_date() 接收“日历”WebElement 和可选的最小日期。此函数将首先在日历中查找 Your date,如果它在那里并且大于最小值(如果给定),则单击它并返回日期。如果没有 Your date,查找可用的“航类”天数,如果给出了最短日期并且日期大于或等于它,则单击它并返回日期。

工作实现:

from datetime import datetime, timedelta

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def select_date(calendar, mininum_date=None):
try:
# check if "Your Date" is there
your_date_elm = calendar.find_element_by_class_name("your-date")

your_date = your_date_elm.get_attribute("data-date")
print("Found 'Your Date': " + your_date)
your_date_elm.click()

# check if your_date against the minimum date if given
your_date = datetime.strptime(your_date, "%Y-%m-%d")
if mininum_date and your_date < mininum_date:
raise NoSuchElementException("Minimum date violation")
return your_date
except NoSuchElementException:
flight_date = None
flight_date_elm = None
while True:
print("Processing " + calendar.find_element_by_css_selector("div.subheader > p").text)

try:
if mininum_date:
flight_date_elms = calendar.find_elements_by_class_name("flights")
flight_date_elm = next(flight_date_elm for flight_date_elm in flight_date_elms
if datetime.strptime(flight_date_elm.get_attribute("data-date"), "%Y-%m-%d") >= mininum_date)
else:
flight_date_elm = calendar.find_element_by_class_name("flights")
except (StopIteration, NoSuchElementException):
calendar.find_element_by_partial_link_text("Next month").click()

# if found - print out the date, click and exit the loop
if flight_date_elm:
flight_date = flight_date_elm.get_attribute("data-date")
print("Found 'Flight Date': " + flight_date)
flight_date_elm.click()
break

return datetime.strptime(flight_date, "%Y-%m-%d")


driver = webdriver.Firefox()
driver.get("http://www.jet2.com/cheap-flights/leeds-bradford/antalya/2016-03-01/2016-04-12?adults=2&children=2&infants=1&childages=4%2c6")

wait = WebDriverWait(driver, 10)

# get the outbound date
outbound = wait.until(EC.visibility_of_element_located((By.ID, "outboundsearchresults")))
outbound_date = select_date(outbound)

# get the inbound date
inbound = driver.find_element_by_id("inboundsearchresults")
inbound_minimum_date = outbound_date + timedelta(days=7)
inbound_date = select_date(inbound, mininum_date=inbound_minimum_date)

print(outbound_date, inbound_date)

driver.close()

对于问题 URL 中提供的,它打印:

Processing March 2016
Found 'Flight Date': 2016-03-28

Processing April 2016
Found 'Flight Date': 2016-04-04

2016-03-28 00:00:00 2016-04-04 00:00:00

最后打印的两个日期是出发日期和返回日期。

如果您需要任何说明,请告诉我,希望对您有所帮助。

关于python - 如何查看当前日期并转到下一个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35256504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com