Last active
February 21, 2023 13:27
-
-
Save joelibaceta/92a940f58e44c584a6273da293b9cb06 to your computer and use it in GitHub Desktop.
Código de ejemplo de scrapping de un sitio web de ecommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from explorer import get_product_name, get_product_price, get_items | |
products = [] | |
for page in range(1, 13): | |
product_items = get_items(page=page) | |
for product_item in product_items: | |
try: | |
price = get_product_price(product_item) | |
name = get_product_name(product_item) | |
print(f"{name}: {price}") | |
products.append( | |
{ | |
"price": price, | |
"name": name | |
} | |
) | |
except: | |
pass | |
precio_minimo = products[0]["price"] | |
precio_maximo = products[0]["price"] | |
sumatoria = 0 | |
for product in products: | |
sumatoria = sumatoria + product["price"] | |
if product["price"] < precio_minimo: | |
precio_minimo = product["price"] | |
if product["price"] > precio_maximo: | |
precio_maximo = product["price"] | |
promedio = sumatoria / len(products) | |
print(f"El precio minimo de una laptop es: {precio_minimo}") | |
print(f"El precio maximo de una laptop es: {precio_maximo}") | |
print(f"El precio promedio de una laptop es: {promedio}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.webdriver.common.by import By | |
from scrapper import run | |
import time | |
driver = run() | |
def get_items(page=1): | |
driver.get(f"https://simple.ripley.com.pe/tecnologia/computacion/laptops?s=mdco&page={page}") | |
time.sleep(5) | |
return driver.find_elements(by=By.CLASS_NAME, value="catalog-product-item") | |
def get_product_price(product_item): | |
price_string = product_item.find_element(by=By.CLASS_NAME, value="catalog-prices__offer-price").text | |
symbol, amount = price_string.split(" ") | |
amount = amount.replace(",", "") | |
return float(amount) | |
def get_product_name(product_item): | |
return product_item.find_element(by=By.CLASS_NAME, value="catalog-product-details__name").text | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
def run(): | |
DRIVER_PATH = "./chromedriver" | |
driver = webdriver.Chrome(executable_path=DRIVER_PATH) | |
return driver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment