Разработка бота-парсера акций и скидок конкурентов
Мониторинг акций конкурентов позволяет быстро реагировать: запустить контракцию, подготовить собственное предложение или скорректировать маркетинговый календарь. Акции — сложнее для парсинга, чем цены: они имеют даты начала/конца, условия, типы скидок.
Что отслеживается
- Процентные скидки — "скидка 30% на весь каталог"
- Акционные цены — конкретная цена на конкретный товар
- Промокоды — публичные промокоды конкурентов
- Временные акции — flash-sale, акции выходного дня
- Программы лояльности — кешбэк, бонусные баллы
- Бандлы — "2+1 бесплатно", комплекты со скидкой
Парсер страниц акций
// app/Services/PromotionScraper/PromotionPageScraper.php
class PromotionPageScraper
{
public function scrapePromotionsPage(string $url): array
{
$html = $this->fetch($url);
$crawler = new Crawler($html);
$promotions = [];
// Стандартные блоки акций
$crawler->filter('.promotion-card, .sale-block, .promo-item, [data-promo]')
->each(function (Crawler $node) use (&$promotions) {
$promo = $this->extractPromotion($node);
if ($promo) $promotions[] = $promo;
});
// Если структурированные блоки не найдены — парсим текстом
if (empty($promotions)) {
$promotions = $this->extractFromText($crawler->text(), $url);
}
return $promotions;
}
private function extractPromotion(Crawler $node): ?array
{
$title = $node->filter('h2, h3, .promo-title, .sale-title')->first()->text('');
if (empty(trim($title))) return null;
$description = $node->filter('p, .promo-desc')->first()->text('');
$link = $node->filter('a')->first()->attr('href') ?? '';
// Извлекаем даты из текста
$dates = $this->extractDates($title . ' ' . $description);
// Извлекаем процент скидки
$discount = $this->extractDiscount($title . ' ' . $description);
// Ищем промокод в тексте
$promoCode = $this->extractPromoCode($title . ' ' . $description);
return [
'title' => trim($title),
'description' => trim($description),
'discount_pct'=> $discount,
'promo_code' => $promoCode,
'starts_at' => $dates['start'] ?? null,
'ends_at' => $dates['end'] ?? null,
'url' => $link,
];
}
private function extractDiscount(string $text): ?int
{
// "скидка 30%", "−30%", "30% OFF", "до 50% скидки"
if (preg_match('/[-–]?\s*(\d{1,3})\s*%/u', $text, $m)) {
return (int) $m[1];
}
return null;
}
private function extractPromoCode(string $text): ?string
{
// Промокод обычно uppercase, 4-12 символов, иногда в кавычках или после слова "промокод"
if (preg_match('/промокод[:\s]+([A-Z0-9_-]{3,15})/ui', $text, $m)) {
return strtoupper($m[1]);
}
if (preg_match('/promo(?:code)?[:\s]+([A-Z0-9_-]{3,15})/i', $text, $m)) {
return strtoupper($m[1]);
}
// Слова в кавычках похожие на промокод
if (preg_match('/[«"\'"]([A-Z0-9_-]{4,12})[»"\'"]/u', $text, $m)) {
return strtoupper($m[1]);
}
return null;
}
private function extractDates(string $text): array
{
$dates = [];
// "с 01.03 по 31.03", "до 31 марта", "01.03.2025 - 15.03.2025"
$monthMap = [
'января'=>'01','февраля'=>'02','марта'=>'03','апреля'=>'04',
'мая'=>'05','июня'=>'06','июля'=>'07','августа'=>'08',
'сентября'=>'09','октября'=>'10','ноября'=>'11','декабря'=>'12',
];
$pattern = '/(\d{1,2})\s+(' . implode('|', array_keys($monthMap)) . ')/ui';
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $i => $match) {
$day = sprintf('%02d', $match[1]);
$month = $monthMap[mb_strtolower($match[2])];
$year = date('Y');
$date = "{$year}-{$month}-{$day}";
if ($i === 0) $dates['start'] = $date;
if ($i === 1) $dates['end'] = $date;
}
}
return $dates;
}
}
Мониторинг скидок на конкретных товарах
// app/Services/PromotionScraper/ProductSaleDetector.php
class ProductSaleDetector
{
public function detectSale(string $html): ?SaleInfo
{
$crawler = new Crawler($html);
// Ищем одновременно старую и новую цену
$originalPriceNode = $crawler->filter(
'.original-price, .old-price, del, [data-original-price], s'
)->first();
$salePriceNode = $crawler->filter(
'.sale-price, .special-price, .discount-price, [data-sale-price]'
)->first();
if (!$originalPriceNode->count() || !$salePriceNode->count()) {
return null;
}
$originalPrice = $this->parsePrice($originalPriceNode->text());
$salePrice = $this->parsePrice($salePriceNode->text());
if ($originalPrice <= 0 || $salePrice <= 0 || $salePrice >= $originalPrice) {
return null;
}
$discountPct = round((1 - $salePrice / $originalPrice) * 100);
// Срок действия акции
$endDate = null;
$countdownNode = $crawler->filter('.countdown, [data-countdown], .sale-ends');
if ($countdownNode->count()) {
$endDate = $countdownNode->first()->attr('data-end-date')
?? $this->extractDateFromText($countdownNode->first()->text());
}
return new SaleInfo(
originalPrice: $originalPrice,
salePrice: $salePrice,
discountPct: $discountPct,
endsAt: $endDate,
);
}
}
Хранение и история акций
// Миграция
Schema::create('competitor_promotions', function (Blueprint $table) {
$table->id();
$table->foreignId('competitor_id')->constrained();
$table->string('title');
$table->text('description')->nullable();
$table->integer('discount_pct')->nullable();
$table->string('promo_code')->nullable();
$table->string('source_url');
$table->date('starts_at')->nullable();
$table->date('ends_at')->nullable();
$table->boolean('is_active')->default(true);
$table->json('affected_categories')->nullable();
$table->timestamp('first_seen_at');
$table->timestamp('last_seen_at');
$table->timestamps();
$table->index(['competitor_id', 'is_active', 'ends_at']);
});
// app/Jobs/ScrapeCompetitorPromotions.php
class ScrapeCompetitorPromotions implements ShouldQueue
{
public function handle(PromotionPageScraper $scraper): void
{
$competitor = Competitor::findOrFail($this->competitorId);
$promotionUrls = $competitor->promotion_urls ?? [];
$currentPromos = [];
foreach ($promotionUrls as $url) {
$scraped = $scraper->scrapePromotionsPage($url);
$currentPromos = array_merge($currentPromos, $scraped);
sleep(rand(2, 4));
}
// Деактивируем акции, которых больше нет
CompetitorPromotion::where('competitor_id', $this->competitorId)
->where('is_active', true)
->whereNotIn('source_url', array_column($currentPromos, 'url'))
->update(['is_active' => false]);
// Обновляем или создаём акции
foreach ($currentPromos as $promo) {
CompetitorPromotion::updateOrCreate(
[
'competitor_id' => $this->competitorId,
'source_url' => $promo['url'],
],
[
'title' => $promo['title'],
'description' => $promo['description'],
'discount_pct' => $promo['discount_pct'],
'promo_code' => $promo['promo_code'],
'starts_at' => $promo['starts_at'],
'ends_at' => $promo['ends_at'],
'is_active' => true,
'last_seen_at' => now(),
'first_seen_at' => now(), // updateOrCreate сохранит при create
]
);
}
// Уведомление о новых крупных акциях
$newBigSales = CompetitorPromotion::where('competitor_id', $this->competitorId)
->where('first_seen_at', '>=', now()->subMinutes(10))
->where('discount_pct', '>=', 20)
->get();
if ($newBigSales->isNotEmpty()) {
Notification::route('slack', config('monitoring.slack_url'))
->notify(new BigCompetitorSaleNotification($competitor, $newBigSales));
}
}
}
Расписание
// Акции — чаще, особенно в пятницу/выходные
$schedule->command('scrape:promotions')
->everyTwoHours()->withoutOverlapping();
// Перед выходными — повышенная частота
$schedule->command('scrape:promotions')
->fridays()->at('09:00');
Срок разработки: парсер акций для 3-5 конкурентов с историей, уведомлениями в Slack и дашбордом — 5-8 рабочих дней.







