temp-codi / back-end

recommends clothes to wear based on current temperature + event + mbti type
MIT License
1 stars 1 forks source link

retrieve data from openWeatherApi website using web-scraping (cheerio) #1

Open pol-dev-shinroo opened 1 year ago

pol-dev-shinroo commented 1 year ago

tables of weather codes, descriptions we can use : https://openweathermap.org/weather-conditions

pol-dev-shinroo commented 1 year ago
const cheerio = require("cheerio");
const axios = require("axios");

const url = "https://openweathermap.org/weather-conditions";

const getScrapeData = async () => {
  const response = await axios.get(url);
  const $ = cheerio.load(response.data);

  let hashMap = {};

  for (let i = 1; i < 8; i++) {
    const contaner = $(`table:eq(${i}) tr`);
    const arr = [];
    contaner.each(function (i) {
      const id = $(this).find("td:nth-child(1)").text().trim();
      const main = $(this).find("td:nth-child(2)").text().trim();
      const desc = $(this).find("td:nth-child(3)").text().trim();
      if (id !== "") {
        const obj = { id, main, desc };
        arr.push(obj);
      }
    });
    hashMap[i] = arr;
  }
  console.log(hashMap);
};

module.exports = getScrapeData;
pol-dev-shinroo commented 1 year ago

Image