试图抓取酒店预订网站.无法取回奖品 [英] Trying to scrape a hotel booking website. Not able to retrieve the prize

查看:28
本文介绍了试图抓取酒店预订网站.无法取回奖品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考这个问题.试图抓取该网站

预期输出是:

酒店名称

第一个安排选项:它的奖品

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

https://i.stack.imgur.com/FdYJX.png

这篇关于试图抓取酒店预订网站.无法取回奖品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆