Çözüldü Node.js Web Scraping null değer döndürüyor

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.
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?

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:
Çözüm
Sorunu hallettim, çalışıyor şu an.
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/";
const fetchData = async (query) => {
  try {
    let products = [];
    const url = base_url + query;
    let res = await axios.get(url);
    let $ = 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,
        });
      }
    });
    return products;
  } catch (e) {
    console.log(e);
  }
};
app.get("/products/:query", async (req, res) => {
  const query = req.params.query; // Kullanıcının girdiği sorguyu al
  products = []; // Her yeni sorguda ürünleri temizle
  const data = await fetchData(query); // Yeni sorguya göre verileri çek
  res.json(data);
});
app.listen(port, () => console.log("Server running"));
Ekran görüntüsü 2023-09-22 190949.png
Sorunu hallettim, çalışıyor şu an.
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/";
const fetchData = async (query) => {
  try {
    let products = [];
    const url = base_url + query;
    let res = await axios.get(url);
    let $ = 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,
        });
      }
    });
    return products;
  } catch (e) {
    console.log(e);
  }
};
app.get("/products/:query", async (req, res) => {
  const query = req.params.query; // Kullanıcının girdiği sorguyu al
  products = []; // Her yeni sorguda ürünleri temizle
  const data = await fetchData(query); // Yeni sorguya göre verileri çek
  res.json(data);
});
app.listen(port, () => console.log("Server running"));
Ekran görüntüsü 2023-09-22 190949.png
 
Çözüm
Sorunu hallettim, çalışıyor şu an.
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/";
const fetchData = async (query) => {
 try {
 let products = [];
 const url = base_url + query;
 let res = await axios.get(url);
 let $ = 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,
 });
 }
 });
 return products;
 } catch (e) {
 console.log(e);
 }
};
app.get("/products/:query", async (req, res) => {
 const query = req.params.query; // Kullanıcının girdiği sorguyu al.
 products = []; // Her yeni sorguda ürünleri temizle.
 const data = await fetchData(query); // Yeni sorguya göre verileri çek.
 res.json(data);
});
app.listen(port, () => console.log("Server running"));
Eki Görüntüle 1953224

Çok teşekkür ederim.
 
Çok teşekkür ederim.
Öncelikle fetchData asynchronous olduğu için await kullanmayı unutmayın. Ayrıca products değişkenini global olarak declare etmek hata, direkt fetchData fonksiyonunun return değeri olarak ayarlamanız gerekiyor.

Bu satıra da gerek yok. Silmeyi unutmuşum.
JavaScript:
products = []; // Her yeni sorguda ürünleri temizle.
 

Geri
Yukarı