0

I am a beginner in programming. I am using nodejs. I am web scraping details of some products from a website.The axios in my code is inside a for loop, iterating over each url, corresponding to each product.I am pushing the response into a variable.I am getting the response inside axios but when the variable is printed outside of axios, its giving an empty array. How can I make the response available outside also.

app.get('/results',(req,res)=>{
 const website_url=["url1", "url2","url3", "url4"];
 const final_result=[];
 for(let i=0;i<4;i++){
   const url=website_url[i];
   axios(url).then(response=>{
     const html=response.data;
     const $=cheerio.load(html);
     const articles=[];
     const rating=[];
     $('.a-offscreen',html).each(function(){
        const price=$(this).text();
        articles.push({price:price
        });
     });
    final_result.push(articles[4], articles[0]);
    }).catch(err=>console.log(err));
  }
  res.json(final_result);
});
chitown88
  • 27,527
  • 4
  • 30
  • 59
aea
  • 21
  • 4

0 Answers0