From 32c1617f7dd3686f3109042edb34732495a0eac8 Mon Sep 17 00:00:00 2001 From: Anatoly Bogomolov Date: Wed, 20 Dec 2023 12:38:43 +1000 Subject: [PATCH] store parsing --- stolichki/product.py | 48 +++++++++++++++++++++---------------- stolichki/types/__init__.py | 1 + stolichki/types/store.py | 9 +++++++ 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 stolichki/types/__init__.py create mode 100644 stolichki/types/store.py diff --git a/stolichki/product.py b/stolichki/product.py index ab39a39..151cc94 100644 --- a/stolichki/product.py +++ b/stolichki/product.py @@ -1,11 +1,12 @@ import logging +import dataclasses from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from .browser import StolichkiDriver - +from .types import Store class Product: id: int = 0 @@ -48,11 +49,6 @@ class Product: def __parse_stores(self): try: self.driver.find_element(By.CSS_SELECTOR, "p.badge-class.product-not-found") - return [] - except: - pass - - try: self.driver.find_element(By.CSS_SELECTOR, "a.stores-stock.stores-order.package") return [] except: @@ -62,20 +58,32 @@ class Product: wait = WebDriverWait(self.driver, 30) wait.until(EC.presence_of_element_located((By.CLASS_NAME, "tr-start-store"))) + + reg_stores = r"https://stolichki\.ru/drugs/\d{1,}/stores\?cityId=\d{1,}&no-captcha-token=.{1,}" + response = self.driver.get_network_response(reg_stores) - stores = self.driver.find_elements(By.CLASS_NAME, "tr-start-store") - stores_list = [] - for store in stores: - try: - store_name = store.find_element(By.CLASS_NAME, "store-link").text - number_of_product = int(store.find_element(By.CLASS_NAME, "part-quantity").text) + for store in response.get("stores"): + if store.get("parts"): + prices = store.get("parts")[0] + + store_normal = Store( + id = store.get("id"), + name=store.get("name"), + address=store.get("address"), + price=prices.get("priceStore"), + price_order=prices.get("priceOnline") + ) + + self.stores.append(store_normal) - stores_list.append({ - "name": store_name, - "quantity": number_of_product - }.copy()) - - except: - continue + if bool(prices.get("bad")): + store_special = dataclasses.replace(store_normal) + store_special.name += " СП" - return stores_list + discounts = prices.get("discounts") + discount = discounts[0].get("value") + + store_special.price = store_normal.price - (store_normal.price * (discount / 100)) + store_special.price_order = store_normal.price_order - (store_normal.price_order * (discount / 100)) + + self.stores.append(store_special) \ No newline at end of file diff --git a/stolichki/types/__init__.py b/stolichki/types/__init__.py new file mode 100644 index 0000000..29796eb --- /dev/null +++ b/stolichki/types/__init__.py @@ -0,0 +1 @@ +from .store import * \ No newline at end of file diff --git a/stolichki/types/store.py b/stolichki/types/store.py new file mode 100644 index 0000000..e60e49c --- /dev/null +++ b/stolichki/types/store.py @@ -0,0 +1,9 @@ +from dataclasses import dataclass + +@dataclass +class Store(): + id: int + name: str + address: str + price: float = 0.0 + price_order: float = 0.0 \ No newline at end of file