I'm creating an API using the Cheerio library to do a webscrapping on the imdb site and get a movie ranking. I catch all the information on the site and send it to the items array as an object and I would like to return this in json to the user making a get request. The web scrapping is working, because in the console.log(items)
inside var cheerioData
returns me all the items I would like. But putting this console.log outside of axios, it returns an empty array as I make the request and I get a status 200 with the array empty.
import * as cheerio from 'cheerio';
import { Request, Response } from "express";
import axios from 'axios';
class ScrappingRaking {
async index(req: Request, res: Response) {
const items: object [] = [];
try {
axios("https://www.imdb.com/chart/tvmeter/?ref_=nv_tvv_mptv").then(res => {
const data = res.data;
const $ = cheerio.load(data);
var cheerioData = [...$('.lister-list>tr')].map((e, i) => {
if (i < 15) {
const ranking = i + 1;
const title = $(e).find('.titleColumn a').text().trim();
const year = $(e).find('.titleColumn span').text().trim();
items.push({ranking, title, year});
console.log(items);
}
})
})
return res.status(200).json(items);
} catch (err) {
return res.status(500).json(err);
}
}
}
export default new ScrappingRaking();
How could I make the items returned inside my array be displayed to my user by a req get in status 200