【发布时间】:2021-05-22 21:58:43
【问题描述】:
我需要一个解决方案来解决我的代码,我试图抓取一个动态网页调用 easy.cl 并且只得到 4 个项目,有时没有(仅当我下载标题时,无法下载价格,因为不显示任何东西)。好吧,无论如何,我需要一个关于我的错误在哪里的指南,因为 Selenium 在我的结果中没有显示任何内容(Sublime text3)。此外,easy.cl 是动态的,带有一个按钮来显示更多信息产品。最后,我正在考虑滚动解决方案,但不知道,你会在我的位置做什么?有什么提示可以尝试找到解决方案吗?
提前,谢谢。
import random
from time import sleep
from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
#baseUrl = 'https://www.easy.cl'
driver.get('https://www.easy.cl/tienda/categoria/ceramicas?cur_page=1&cur_view=grid&bc_links=pisos-y-muros&bc_names=Pisos')
#boton = driver.find_element_by_xpath('//button[@class="primary_plp load-more-products"]')
inicio = []
for i in range(5):
try:
boton = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//button[@class="primary_plp load-more-products"]')))
boton.click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH,'//a[@class="product_image"]')))
except:
break
# Espero que carguen los productos...
links_productos = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,'//a[@class="product_image"]')))
# obtengo los links de los detalles de los productos
links_pagina=[]
for tag_a in links_productos:
links_pagina.append(tag_a.get_attribute("href"))
for link in links_pagina:
try:
driver.get(link)
titulo = driver.find_element(By.XPATH, '//h1[@class="product-details__title"]').text
#if driver.find_element(By.XPATH, '//span[@class="priceNumber priceNumberAlignRight"]'):
# precio = find_element_by_xpath('//span[@class="priceNumber priceNumberAlignRight"]').text
#else:
# precio = "Sin precio M2"
print(titulo)
#print(precio)
except:
break
【问题讨论】:
标签: python-3.x selenium selenium-webdriver web-scraping