Recep Yıldırım
Hectopat
- Katılım
- 10 Temmuz 2020
- Mesajlar
- 486
- Çözümler
- 2
Daha fazla
- Cinsiyet
- Erkek
Arkadaşlar kahvesepeti adlı siteden kullanıcın girdiği query e göre listeleme yapan bir scrape kodu yazmaya çalıştım ama ne yazarsam yazayım boş bir result çıkıyor.
Neden acaba?
Neden acaba?
JavaScript:
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const app = express();
const port = process.env.PORT || 5000;
const base_url = 'https://www.kahvesepeti.com/arama/';
let products = [];
const fetchData = async (query) => {
try {
const url = base_url + query;
let res = await axios.get(url);
let $ = await cheerio.load(res.data);
$("#results-page > div.showcase-container > div.row > div.col-6.col-lg-4 > div.showcase").each((i, e) => {
const title = $(e).find('.showcase-title').text().trim();
const price = $(e).find('.showcase-price-new').text().trim();
const oprice = $(e).find('.showcase-price-old').text().trim();
const img = $(e).find('.showcase-image a img').attr('data-src');
const link = $(e).find('.showcase-image a').attr('href');
// Eğer hem başlık hem de fiyat dolu ise, ürünü ekleyin
if (title && price && oprice && img && link) {
products.push({
title: title,
newPrice: price,
oldPrice: oprice,
currency: "TL",
image: img,
url: link
});
}
});
// Eğer hiç ürün bulunamazsa, products dizisini boşalt
if (products.length === 0) {
products = [];
}
} catch (e) {
console.log(e);
}
}
app.get('/products/:query', (req, res) => {
const query = req.params.query; // Kullanıcının girdiği sorguyu al
products = []; // Her yeni sorguda ürünleri temizle
fetchData(query); // Yeni sorguya göre verileri çek
res.json(products);
});
app.listen(port, () => console.log("Server running"));
Son düzenleme: