Загрузка данных
# ==========================================
# ПАРСЕР WILDBERRIES ДЛЯ GOOGLE COLAB
# Selenium + Requests + Pandas
# ==========================================
# 1. УСТАНОВКА
import subprocess
import sys
subprocess.run(
["apt-get", "update", "-qq"],
stdout=subprocess.DEVNULL,
check=True
)
subprocess.run(
["apt-get", "install", "-y", "-qq", "chromium", "chromium-driver"],
stdout=subprocess.DEVNULL,
check=True
)
subprocess.check_call([
sys.executable, "-m", "pip", "install", "-q",
"selenium", "requests", "pandas"
])
# 2. ИМПОРТЫ
import time
import shutil
import requests
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from google.colab import files
# ==========================================
# НАСТРОЙКИ
# ==========================================
SEARCH = "ноутбук"
PAGES = 3
API_URL = (
"https://www.wildberries.ru/"
"__internal/u-search/exactmatch/ru/common/v18/search"
)
# ==========================================
# 3. ЗАПУСК SELENIUM
# ==========================================
print("Запускаем браузер...")
options = Options()
options.binary_location = (
shutil.which("chromium")
or "/usr/bin/chromium"
)
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(
service=Service(shutil.which("chromedriver")),
options=options
)
# ==========================================
# 4. ПОЛУЧЕНИЕ COOKIE
# ==========================================
cookies = {}
try:
print("Открываем Wildberries...")
driver.get("https://www.wildberries.ru/")
time.sleep(8)
print("Заголовок:", driver.title)
for cookie in driver.get_cookies():
cookies[cookie["name"]] = cookie["value"]
token = cookies.get("x_wbaas_token")
if token:
print("Cookie x_wbaas_token получена!")
else:
print("Cookie x_wbaas_token отсутствует.")
finally:
driver.quit()
# ==========================================
# 5. СОЗДАЁМ СЕССИЮ
# ==========================================
session = requests.Session()
session.cookies.update(cookies)
session.headers.update({
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/142.0.0.0 Safari/537.36"
),
"Accept": "application/json, text/plain, */*",
"Referer": "https://www.wildberries.ru/",
"Origin": "https://www.wildberries.ru"
})
# ==========================================
# 6. ПАРСИНГ ТОВАРОВ
# ==========================================
products = []
for page in range(1, PAGES + 1):
print(f"\nСтраница {page}/{PAGES}")
params = {
"appType": 1,
"curr": "rub",
"dest": -1275551,
"lang": "ru",
"page": page,
"query": SEARCH,
"resultset": "catalog",
"sort": "popular",
"spp": 30
}
try:
response = session.get(
API_URL,
params=params,
timeout=30
)
print("HTTP:", response.status_code)
if response.status_code != 200:
print("Сервер отклонил запрос.")
print(response.text[:200])
break
data = response.json()
items = data.get("products")
if items is None:
items = data.get("data", {}).get("products", [])
print("Найдено товаров:", len(items))
for item in items:
# Получение цены
price = item.get("salePriceU")
if price is None:
sizes = item.get("sizes", [])
if sizes:
price_info = sizes[0].get("price") or {}
price = price_info.get("product")
if price is not None:
price = price / 100
product = {
"Артикул": item.get("id"),
"Название": item.get("name"),
"Бренд": item.get("brand"),
"Цена": price,
"Рейтинг": item.get("rating"),
"Отзывы": item.get("feedbacks"),
"Ссылка": (
f"https://www.wildberries.ru/catalog/"
f"{item.get('id')}/detail.aspx"
)
}
products.append(product)
time.sleep(3)
except Exception as e:
print("Ошибка:", e)
break
# ==========================================
# 7. СОЗДАНИЕ DATAFRAME
# ==========================================
df = pd.DataFrame(products)
if not df.empty:
df = df.drop_duplicates(
subset=["Артикул"]
)
print("\nВсего товаров:", len(df))
display(df.head(20))
# ======================================
# 8. СОХРАНЕНИЕ CSV
# ======================================
filename = "wildberries.csv"
df.to_csv(
filename,
index=False,
encoding="utf-8-sig"
)
print("CSV сохранён!")
files.download(filename)
else:
print("\nНе удалось получить товары.")
print("Проверьте доступность API и cookie.")