store parsing

This commit is contained in:
Анатолий Богомолов 2023-12-20 12:38:43 +10:00
parent 41e42e8b57
commit 32c1617f7d
3 changed files with 38 additions and 20 deletions

View File

@ -1,11 +1,12 @@
import logging import logging
import dataclasses
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from .browser import StolichkiDriver from .browser import StolichkiDriver
from .types import Store
class Product: class Product:
id: int = 0 id: int = 0
@ -48,11 +49,6 @@ class Product:
def __parse_stores(self): def __parse_stores(self):
try: try:
self.driver.find_element(By.CSS_SELECTOR, "p.badge-class.product-not-found") 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") self.driver.find_element(By.CSS_SELECTOR, "a.stores-stock.stores-order.package")
return [] return []
except: except:
@ -63,19 +59,31 @@ class Product:
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "tr-start-store"))) wait.until(EC.presence_of_element_located((By.CLASS_NAME, "tr-start-store")))
stores = self.driver.find_elements(By.CLASS_NAME, "tr-start-store") reg_stores = r"https://stolichki\.ru/drugs/\d{1,}/stores\?cityId=\d{1,}&no-captcha-token=.{1,}"
stores_list = [] response = self.driver.get_network_response(reg_stores)
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)
stores_list.append({ for store in response.get("stores"):
"name": store_name, if store.get("parts"):
"quantity": number_of_product prices = store.get("parts")[0]
}.copy())
except: store_normal = Store(
continue id = store.get("id"),
name=store.get("name"),
address=store.get("address"),
price=prices.get("priceStore"),
price_order=prices.get("priceOnline")
)
return stores_list self.stores.append(store_normal)
if bool(prices.get("bad")):
store_special = dataclasses.replace(store_normal)
store_special.name += " СП"
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)

View File

@ -0,0 +1 @@
from .store import *

9
stolichki/types/store.py Normal file
View File

@ -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