简体   繁体   English

如何从元素列表中提取数据 python

[英]how to extract data from an element list python

I am working on a project that really blocked me I often asked questions here and you have helped me a lot since I am still a beginner, my project consists in making a competitive watch table for hotel rates for an agency It is a painful action that I wanted to automate it, I succeeded in extracting the tariffs and their prices, but the problem is that I want him to give me only the selected room I provide you with the code and the output i removed the data that i want to elimnate in my output also i've addede images to better clarify things if any of you can help me and thank you in advance.我正在做一个真正阻碍我的项目我经常在这里问问题而且你帮助了我很多因为我还是一个初学者,我的项目包括为一家机构制作一个具有竞争力的酒店价格观察表这是一个痛苦的行动我想让它自动化,我成功地提取了关税和它们的价格,但问题是我希望他只给我我提供给你的选定房间代码和 output 我删除了我想删除的数据我的 output 我也添加了图片以更好地澄清事情,如果你们中的任何人可以帮助我并提前感谢你。

NB: thanks to pmadhu's answer problem solved but now it shows me the same rates for all hotels.注意:感谢 pmadhu 的回答,问题已解决,但现在它显示所有酒店的价格相同。

在此处输入图像描述

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

# 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': 'Nabeul',
    'date_from': '24/08/2021',
    'date_to': '25/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)

# submit form
btn_rechercher = driver.find_element_by_id('boutonr')
btn_rechercher.click()
sleep(10)

# ----------------------------------------------------------------------------
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import StaleElementReferenceException,NoSuchElementException
urls = []
hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
    link = hotel.find_element_by_xpath(".//span[@class='tittre_hotel']/a").get_attribute("href")
    urls.append(link)
for url in urls:
    driver.get(url)
    try:
        name = driver.find_element_by_xpath("//div[@class='bloc_titre_hotels']/h2").text
        arropt = driver.find_element_by_xpath("//div[contains(@class,'line_result')][1]")
        opt = arropt.find_element_by_tag_name("b").text
        num = len(arropt.find_elements_by_tag_name("option"))
        optiondata = {}
        achats = {}
        marges= {}
        selection = Select(driver.find_element_by_id("arrangement"))
        for i in range(num):
            try:
                selection = Select(driver.find_element_by_id("arrangement"))
                selection.select_by_index(i)
                time.sleep(2)
                arr = driver.find_element_by_xpath("//select[@id='arrangement']/option[@selected='selected']").text
                prize = driver.find_element_by_id("prix_total").text
                
                optiondata[arr]=prize

                
                btn_passe = driver.find_element_by_xpath('//*[@id="resultat"]/div/form/div/div[2]/div[1]/div[2]/div[2]/div/div ')
                btn_passe.click()
                sleep(2)
                                    # params to select
                params = {
                            'civilite_acheteur': 'Mlle',
                            'prenom_acheteur': 'test',
                            'nom_acheteur': 'test',
                            'e_mail_acheteur': 'test@gmail.com',
                            'portable_acheteur': '22222222'
                        }

                        # select civilite
                civilite_acheteur = Select(driver.find_element_by_id('civilite_acheteur'))
                civilite_acheteur.select_by_value(params['civilite_acheteur'])

                        # saisir prenom 
                script = f"document.getElementById('prenom_acheteur').value ='{params['prenom_acheteur']}';"
                script += f"document.getElementById('nom_acheteur').value ='{params['nom_acheteur']}';"
                script += f"document.getElementById('e_mail_acheteur').value ='{params['e_mail_acheteur']}';"
                script += f"document.getElementById('portable_acheteur').value ='{params['portable_acheteur']}';"
                driver.execute_script(script)

                        # submit form
                btn_rechercher = driver.find_element_by_id('titre_Hammamet')
                btn_rechercher.click()
                sleep(2)

                btn_rechercher = driver.find_element_by_id('boutonr')
                btn_rechercher.click()
                sleep(3)
                
                achat = driver.find_element_by_xpath('/html/body/header/div[2]/div[1]/div[1]/div[4]/div[2]/div[2]').text.replace(' TND', '')
                achats[arr]=achat

                marge =int(((float(prize) - float(achat)) / float(achat)) * 100);

                marges[arr]=marge
                optiondata[arr]=prize,achat,marge
                
                driver.get(url)
                btn_passe = driver.find_element_by_xpath('//*[@id="moteur_rech"]/form/div/div[3]/div')
                btn_passe.click()
                sleep(2)
                

            except StaleElementReferenceException:
                pass
            
       
    except NoSuchElementException:
        pass
    print("{} : {} - {}".format(name,opt,optiondata))
    

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Try below code once:尝试以下代码一次:

from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import StaleElementReferenceException,NoSuchElementException

hotels = driver.find_elements_by_xpath("//div[starts-with(@id,'produit_affair')]")
for hotel in hotels:
    link = hotel.find_element_by_xpath(".//span[@class='tittre_hotel']/a").get_attribute("href")
    urls.append(link)
for url in urls:
    driver.get(url)
    try:
        name = driver.find_element_by_xpath("//div[@class='bloc_titre_hotels']/h2").text
        arropt = driver.find_element_by_xpath("//div[contains(@class,'line_result')][1]")
        opt = arropt.find_element_by_tag_name("b").text
        num = len(arropt.find_elements_by_tag_name("option"))
        optiondata = {}
        selection = Select(driver.find_element_by_id("arrangement"))
        for i in range(num):
            try:
                selection = Select(driver.find_element_by_id("arrangement"))
                selection.select_by_index(i)
                time.sleep(2)
                arr = driver.find_element_by_xpath("//select[@id='arrangement']/option[@selected='selected']").text
                prize = driver.find_element_by_id("prix_total").text
                optiondata[arr]=prize
            except StaleElementReferenceException:
                pass
    except NoSuchElementException:
        pass
    print("{} : {} - {} - {}".format(name,opt,num,optiondata))

And the output:和 output:

Tui Blue Scheherazade Sousse : Double Standard Vue Mer - 1 - {'Demi Pension': '114'}
Golf Residence GAS Sousse : Double--Standard - 2 - {'Demi Pension': '51', 'Petit Dejeuner': '42'}
Sindbad Center GAS Sousse : Chambre Double - 2 - {'Petit Dejeuner': '27', 'Logement seul': '22'}

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

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