Python - ayuda Web scraping python

 
Vista:

ayuda Web scraping python

Publicado por nn (1 intervención) el 05/06/2023 17:01:28
Hola! necesito ayuda con web scraping hace muy poco empecé con python, necesito scrappear: https://www.colsubsidio.com/tiendas-en-linea/supermercados de allí tomar los datos de cada ciudad, el nombre de la tienda, dirección, y ciudad, y luego meterlos en un dataframe. no he logrado obtener todos los datos, ya que se supone que se debe ir dando click a la ciudad e ir tomando los datos fila a fila de cada una de las tablas. Esto es lo que he logrado hacer hasta el momento sin embargo en cierto punto salta error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Esperar a que aparezca el banner de cookies
    time.sleep(3)
    wait = WebDriverWait(browser, 10)
    boton = wait.until(EC.element_to_be_clickable((By.ID, 'hs-eu-confirmation-button')))
    boton.click()
 
    tiendas = browser.find_elements(By.CLASS_NAME, "tab.metricas-acordeon")
 
 
    i = 2
    j = 1
    for tienda in tiendas:
       # dar click a cada ciudad para desplegar tabla
        wait = WebDriverWait(browser, 10)
        botonlis = wait.until(EC.visibility_of_element_located(
            (By.XPATH, f'//*[@id="hs_cos_wrapper_widget_47960066412"]/div/div[2]/div/div[{j}]')))
        botonlis.click()
       # tomar todos los elementos de la tabla
        tabla = tienda.find_elements(By.XPATH, f'//*[@id="hs_cos_wrapper_widget_47960066412_"]/table')
        for fila in tabla:
            name = fila.find_element(By.XPATH,
                                     f'//*[@id="hs_cos_wrapper_widget_47960066412_"]/table/tbody/tr[{i}]/td[1]').text
            print(name)
            direccion = fila.find_element(By.XPATH,
                                           f'//*  [@id="hs_cos_wrapper_widget_47960066412_"]/table/tbody/tr[{i}]/td[4]').text
            print(direccion)
            ciudad = fila.find_element(By.XPATH,
                                        f'//*[@id="hs_cos_wrapper_widget_47960066412_"]/table/tbody/tr[{i}]/td[3]').text
            print(ciudad)
            telefono = "sin telefono disponible"
            i += 1
        j +=1

agradezco cualquier consejo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

ayuda Web scraping python

Publicado por antonio (65 intervenciones) el 09/06/2023 21:05:54
Hola buenas aunque salga errores en edge si funciona este código, para que funcione bien debes hacer enter una vez aceptes las cokies de la ventana:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.microsoft import EdgeChromiumDriverManager
 
 
browser = webdriver.Edge(EdgeChromiumDriverManager().install())
browser.get("https://www.colsubsidio.com/tiendas-en-linea/supermercados")
print("Iniciado")
input()//esperar aceptar cokies
tablas = browser.find_elements_by_class_name("tab-label")
 
for i in range(len(tablas)):
    time.sleep(3)
    tablas[i].click()
    tabla = browser.find_elements_by_class_name("tab-content")
    time.sleep(5)
    for i in range(len(tabla)):
        print(tabla[i].text)

Captura

Captura2
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar