从datepicker获取网站x个月的可用性

时间:2016-05-03 16:58:34

标签: python selenium datepicker

我想通过点击日期选择器日历中的下一个按钮来获取airbnb中每天的可用性/价格,但没有运气。

我目前的代码如下:

def handle(self, *args, **options):
    def airbnb():
        display = Display(visible=0, size=(1024, 768))
        display.start()
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get("https://www.airbnb.pt/rooms/265820")

        # wait for the check in input to load
        wait = WebDriverWait(driver, 10)
        elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
        elem.click()

        # wait for datepicker to load
        wait.until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
        )

        days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
        for cell in days:
            day = cell.text.strip()
            if not day:
                continue

            if "ui-datepicker-unselectable" in cell.get_attribute("class"):
                status = "Unavailable"
            else:
                status = "Available"

            price = "n/a"
            if status == "Available":
                # hover the cell and wait for the tooltip
                ActionChains(driver).move_to_element(cell).perform()
                price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text

            print(day, status, price)

他们都工作但只有1个月。我希望能够设置X个月。例如,对于homeaway,我在第一次打开日历点击后立即尝试使用self.driver.find_element_by_css_selector('.ui-datepicker-next.ui-corner-all').c‌​lick(),但我得到了ElementNotVisibleException

提前致谢

1 个答案:

答案 0 :(得分:1)

首先,我会在下个月找到""带有a.ui-datepicker-next CSS选择器的按钮,既可读又可靠。

以下是实现 - 处理MONTH_COUNT变量定义的数月:

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


MONTH_COUNT = 3

driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://www.airbnb.pt/rooms/265820")

# wait for the check in input to load
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.book-it-panel input[name=checkin]")))
elem.click()

# iterate over the month count
for month in range(MONTH_COUNT):
    # wait for datepicker to load
    wait.until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, '.ui-datepicker:not(.loading)'))
    )

    # getting current month for displaying purposes
    current_month = driver.find_element_by_css_selector(".ui-datepicker-month").text
    print(current_month)

    # iterate over days
    days = driver.find_elements_by_css_selector(".ui-datepicker table.ui-datepicker-calendar tr td")
    for cell in days:
        day = cell.text.strip()
        if not day:
            continue

        if "ui-datepicker-unselectable" in cell.get_attribute("class"):
            status = "Unavailable"
        else:
            status = "Available"

        price = "n/a"
        if status == "Available":
            # hover the cell and wait for the tooltip
            ActionChains(driver).move_to_element(cell).perform()
            price = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.datepicker-tooltip'))).text

        print(day, status, price)

    print("-----")
    # click next month
    driver.find_element_by_css_selector("a.ui-datepicker-next").click()

driver.close()

打印:

Maio
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'30', 'Unavailable', 'n/a')
(u'31', 'Unavailable', 'n/a')
-----
Junho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'28', 'Unavailable', 'n/a')
(u'29', 'Unavailable', 'n/a')
(u'30', 'Unavailable', 'n/a')
-----
Julho
(u'1', 'Unavailable', 'n/a')
(u'2', 'Unavailable', 'n/a')
(u'3', 'Unavailable', 'n/a')
...
(u'29', 'Unavailable', 'n/a')
(u'30', 'Available', u'\u20ac36')
(u'31', 'Available', u'\u20ac36')
-----