当前位置: 首页 > 知识库问答 >
问题:

在点击交互代码后抓取数据

陶飞鸿
2023-03-14

我想从一个旅游景点中获取每家酒店的价格,我正在提取名称和安排,但问题是,价格显示了后clic安排,我不知道如何处理它。

我想得到的结果是:

{'Julius':[('Petit Déjeuner','216'),('Demi pension','264')]

如果你们中的任何一个能帮助我,我把我的代码放在你们的处置中,并提前感谢你们。

#!/usr/bin/env python
# coding: utf-8
import json
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait, Select


# create path and start webdriver
PATH = "C:\chromedriver.exe"
driver = webdriver.Chrome(PATH)

# first get website
driver.get('https://tn.tunisiebooking.com/')
wait = WebDriverWait(driver, 20)

# params to select
params = {
    'destination': 'El Jem',
    'date_from': '08/08/2021',
    'date_to': '09/08/2021',
    'bedroom': '1'
}

# select destination
destination_select = Select(driver.find_element_by_id('ville_des'))
destination_select.select_by_value(params['destination'])

# select bedroom
bedroom_select = Select(driver.find_element_by_id('select_ch'))
bedroom_select.select_by_value(params['bedroom'])

# select dates
script = f"document.getElementById('depart').value ='{params['date_from']}';"
script += f"document.getElementById('checkin').value ='{params['date_to']}';"
driver.execute_script(script)

# click bouton search
btn_rechercher = driver.find_element_by_id('boutonr')
btn_rechercher.click()
sleep(10)

# click bouton details
#btn_plus = driver.find_element_by_id('plus_res')
#btn_plus.click()
#sleep(10)

# ----------------------------------------------------------------------------
# get list of all hotels
hotels_list = []
hotels_objects = driver.find_elements_by_xpath(
    '//div[contains(@class, "enveloppe_produit")]'
)
for hotel_obj in hotels_objects:
    # get price object
    price_object = hotel_obj.find_element_by_xpath(
        './/div[@class="monaieprix"]'
    )
    price_value = price_object.find_element_by_xpath(
        './/div[1]'
    ).text.replace('\n', '')

    # get title data
    title_data = hotel_obj.find_element_by_xpath(
        './/span[contains(@class, "tittre_hotel")]'
    )

    # get arrangements
    arrangements_obj = hotel_obj.find_elements_by_xpath(
        './/div[contains(@class, "angle")]//u'
    )
    arrangements = [ao.text for ao in arrangements_obj]
    
    # get arrangements
    prixM_obj = hotel_obj.find_elements_by_xpath(
        './/div[contains(@id, "prixtotal")]'
    )
    prixM = [ao.text for ao in  prixM_obj]

    # create new object
    hotels_list.append({
        'name': title_data.find_element_by_xpath('.//a//h3').text,
        'arrangements': arrangements,
        'prixM':prixM,
        'price': f'{price_value}'
    })

# ----------------------------------------------------------------
#for hotel in hotels_list:
#    print(json.dumps(hotel, indent=4))

import pandas as pd
df = pd.DataFrame(hotels_list, columns=['name','arrangements','price'])
df.head()

共有1个答案

吕成业
2023-03-14

DOM似乎一直在变化。因此,根据此问题和StaleElementReRecommceException的答案,下面的代码可能对您有用。

from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
import time

driver = webdriver.Chrome(executable_path="path")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://tn.tunisiebooking.com/")
#Code to choose options.
hoteldata = {}
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
    details = []
    argmts = hotel.find_element_by_class_name("angle_active").text
    prize = hotel.find_element_by_xpath(".//div[contains(@id,'prixtotal_')]").get_attribute("innerText")
    details.append((argmts,prize))
    inactive = hotel.find_elements_by_xpath(".//div[@class='angle_desactive']")
    for item in inactive:
        try:
            n = item.get_attribute("innerText")
            item.click()
            time.sleep(2)
            pri = hotel.find_element_by_xpath(".//div[contains(@id,'prixtotal_')]").get_attribute("innerText")
            details.append((n,pri))
        except StaleElementReferenceException:
            pass
    hoteldata[name]=details
print(hoteldata)
driver.quit()
 类似资料:
  • 本文向大家介绍vue之数据交互实例代码,包括了vue之数据交互实例代码的使用技巧和注意事项,需要的朋友参考一下 vue中的交互(ajax,jsonp) vue中也存在像ajax和jsonp的数据交互,实现向服务器获取数据,但是他本身框架当中没有这样的方法,需要一个新的小东西叫vue-resouce.js 地址:https://github.com/pagekit/vue-resource/blob

  • 我正试图从这个网页上抓取数据 http://stats.espncricinfo.com/ci/engine/stats/index.html?class=1;团队=5人;模板=结果;类型=击球 我需要从表中复制内容并将其放入csv文件中,然后转到下一页并将这些页面的内容附加到同一文件中。我可以刮表,但是当我尝试使用SeleniumWebDriver的click循环单击next按钮时,它会转到下一

  • "在点击'5更多属性'之前:第一张图片显示了4个div,其中包含我可以使用上述任何方法获得的"span"。 点击'5更多属性'后:第二张图片显示9 div,其中包含我试图到达的"span"。 下面是使用硒/美汤的代码 结果:“跨度”中有990个元素。我只是展示与我的问题相关的东西:

  • 本文向大家介绍node.js 抓取代理ip实例代码,包括了node.js 抓取代理ip实例代码的使用技巧和注意事项,需要的朋友参考一下 node.js实现抓取代理ip 主要文件:index.js 包支持 : package.json 本地需要安装mongodb数据库,用于存储抓取到的ip,目前还未实现ip验证。写这个主要是处于好奇。 上面的代码就可以实现抓取ip代理网站的ip并存到mongodb数

  • 我想从本页的图表中提取数据:http://188.166.44.172/match/live-stats/100941310 我尝试了请求和bs4,但没有获得任何数据,我也尝试了selenium,也没有数据。 以下是使用请求的代码: 和使用硒的代码: 有没有办法用python从这些图表中提取数据?

  • 本文向大家介绍Django封装交互接口代码,包括了Django封装交互接口代码的使用技巧和注意事项,需要的朋友参考一下 我就废话不多说了,大家还是直接看代码吧~ 补充知识:python部署galery集群 galery.py文件内容 以上这篇Django封装交互接口代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。