Untitled
unknown
plain_text
2 years ago
2.0 kB
17
Indexable
import requests
import re
import csv
import pydantic
from pydantic import BaseModel
class parseWB:
def __init__(self, url):
self.brand_id = self.__get_brand_id(url)
@staticmethod
def __get_brand_id(url:str):
regex = "(?<=fbktoys#c).+"
brand_id = re.search(regex, url)[0]
return brand_id
def parse(self):
i = 1
self.__create_csv()
while True:
response = requests.get(
f'https://catalog.wb.ru/brands/f/catalog?appType=1&brand={self.brand_id}&limit=100&page={i}&curr=rub&dest=-1257786®ions=80,38,83,4,64,33,68,70,30,40,86,75,69,22,1,31,66,110,48,71,114&sort=popular&spp=30&uclusters=0'
)
i += 1
items_info = Items.model_validate(response.json()['data'])
if not items_info.products:
break
self.__save_csv(items_info)
def __create_csv(self):
with open ("wb_data11.csv", mode = "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(['id','name','price','brend','sales','rating', 'volume'])
def __save_csv(self, items):
with open("wb_data11.csv", mode="a", newline="") as file:
writer = csv.writer(file)
for product in items.products:
writer.writerow([product.id,
product.name,
product.salePriceU,
product.brand,
product.sale,
product.rating,
product.volume])
class Item(BaseModel):
id: int
name: str
salePriceU: float
brand: str
sale: int
rating: int
volume: int
class Items(BaseModel):
products: list[Item]
if __name__ == "__main__":
parseWB("https://www.wildberries.ru/brands/fbktoys#c140846373").parse()Editor is loading...