试图抓取酒店预订网站.无法取回奖品
问题描述:
预期输出是:
酒店名称
第一个安排选项:它的奖品
First arrangement option : its prize
到目前为止我尝试过的代码:
Code I have tried so far:
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://tn.tunisiebooking.com/")
#code to choose the option "Sousse" ,date- 06/08/21, and click on "Rechercher".
hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
name = hotel.find_element_by_tag_name("h3").text
argmts = hotel.find_element_by_class_name("angle_active").text
prize = hotel.find_element_by_xpath("//div[starts-with(@id,'prixtotal_')]").text
print(name)
print(argmts + ':' + prize)
driver.quit()
我得到的输出:
Petit dejeuner:60
Tui Blue Scheherazade
Demi pension:60
Golf Residence GAS
Petit dejeuner:60
Sindbad Center GAS
Logement seul:60 ...
应用滚动
效果和time.sleep
.但是奖品
保持不变,它没有得到相应酒店的奖品.不知道哪里出了问题.
Applied scrolling
effect and time.sleep
. But the prize
remains same, its doesn't get the prize of the respective Hotel. Not sure where its going wrong.
答
这对我有用,.get_attribute
driver.get("https://tn.tunisiebooking.com/")
select = driver.find_element_by_xpath("//select[@id='ville_des']")
option = Select(select)
option.select_by_index(3)
#code to choose the option "Sousse" ,date- 06/08/21, and click on "Rechercher".
search = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"((//div[@class='col-lg-5 col-md-5 col-sm-5'])[1])")))
search.click()
hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
name = hotel.find_element_by_tag_name("h3").text
argmts = hotel.find_element_by_class_name("angle_active").text
#taken the help of contains() and tried to get the static part of the element ID.
prize = hotel.find_element_by_xpath(".//div[contains(@id,'prixtotal_')]").get_attribute("innerText")
print(name)
print(argmts + ':' + prize)
driver.quit()
O/P