Scraping selenium with html template

Viewed 36

I made an html template that I want to fill with content extracted from a web with python - selenium

import time
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import pandas as pd
import csv 

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))


driver.get('https://es.hoteles.com/ho227810/secrets-lanzarote-resort-spa-adults-only-18-yaiza-espana/')

time.sleep(3)

try:
    cookies = driver.find_element(By.CLASS_NAME, 'osano-cm-button--type_accept')
    cookies.click()
    time.sleep(2)
except:
    pass


try:
    tamano_hotel = driver.find_elements(By.CSS_SELECTOR, 'section#Amenities div.uitk-layout-columns-item')
    hotel=[]
    dictdetails={}
    for iterarhotel in tamano_hotel:
        dictdetails[iterarhotel.find_element(By.XPATH, ".//h3").text]=",".join([item.text for item in iterarhotel.find_elements(By.XPATH, ".//ul//li")])
        hotel.append(dictdetails)
    print(hotel[1])
    time.sleep(2)        
except:
    print("Nothing")

I have extracted a dictionary, but I don't know why it extracts the same content several times and only the first h3.

Would it be possible to extract the content and put it automatically in the html template? The template would be a table and the content would have to go in the second the h3 and in the fourth the items separated by ,

Example:

 <tr>
            <td><i class="fa fa-check red-color"></i></td>
            <td>Hotel size</td>.
            <td><i class="fa fa-arrow-right red-color"></i></td>
             <td>331 rooms, 8 floors</td>.
</tr>

url: https://es.hoteles.com/ho227810/secrets-lanzarote-resort-spa-adults-only-18-yaiza-espana/

img: enter image description here

1 Answers

This is dictionary list, to extract key and value from dictionary items you need to iterate the list and then dictionary items.

for h in hotel:
    for key, value in h.items():
        print("h3 tag text : " + key)
        print("li tag text : " + value) 
Related