简体   繁体   English

尝试在python2.7中使用Selenium获取网站日历数据

[英]Trying to get website calendar data using Selenium in python2.7

I'm trying to get all the calendar data split by unavailable and available for this month and next month.我正在尝试按本月和下个月的不可用和可用来拆分所有日历数据。 I've had no luck in getting the split.我没有运气得到拆分。 Below are the codes I've used.下面是我用过的代码。 I'm either able to get all the dates without the attribute to identify whether those dates are available or unavailable, or when I try to iterate I get an error "selenium.common.exceptions.StaleElementReferenceException: Message: Element is no longer attached to the DOM"我要么能够获得没有属性的所有日期来识别这些日期是否可用或不可用,或者当我尝试迭代时我收到错误“selenium.common.exceptions.StaleElementReferenceException:消息:元素不再附加到DOM”

from selenium import webdriver
url = 'https://www.airbnb.com/rooms/4660676'
driver = webdriver.Firefox()
driver.get(url)
element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH,'/html/body/main/div[3]/div[4]/div/div/div[2]/div[4]/div/div[1]/div/form/div/div/div[2]/button'))
)
element.click()
#This one gets me all the dates 
#listitems=driver.find_elements_by_xpath('/html/body/div[35]/table/tbody/tr[5]')

#If I try something like the following to then be able to get get_attribute then I get the error mentioned above
#available=driver.find_elements_by_xpath('//tr')
#for i in available: print i.text

What would be the way to get the dates.获取日期的方法是什么。 Thanks.谢谢。

Two things to understand to be able to fix it:需要了解两件事才能修复它:

  • the datepicker appears right on the button click, but it takes time to load - wait for it日期选择器出现在按钮点击的右侧,但加载需要时间 - 等待它
  • to determine which of the days is available and not, use the presence of the ui-datepicker-unselectable class要确定哪些天可用而不可用,请使用ui-datepicker-unselectable类的存在

Implementation:执行:

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


url = 'https://www.airbnb.com/rooms/4660676'
driver = webdriver.Firefox()
driver.get(url)

wait = WebDriverWait(driver, 10)

# click "Instant Book"
book = wait.until(
    EC.element_to_be_clickable((By.XPATH,'//button[span = "Instant Book"]'))
)
book.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"

    print(day, status)

Prints:印刷:

1 Unavailable
2 Unavailable
3 Unavailable
4 Unavailable
5 Unavailable
6 Unavailable
7 Unavailable
8 Unavailable
9 Unavailable
10 Unavailable
11 Unavailable
12 Unavailable
13 Unavailable
14 Unavailable
15 Unavailable
16 Unavailable
17 Unavailable
18 Unavailable
19 Unavailable
20 Unavailable
21 Unavailable
22 Unavailable
23 Unavailable
24 Unavailable
25 Unavailable
26 Unavailable
27 Unavailable
28 Available
29 Available
30 Unavailable
31 Unavailable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM