Amazon Scrape returning undefined nightmare package

49 views Asked by At

I am using nightmare.js to get the price for all the products in the database. When I try running just

  const data = await monitor.find({})
  data.forEach(async (monitor1) => {
    const url = monitor1.Link
    console.log(url)
  })

It works, and console.logs many links, but when I try

 data.forEach(async (monitor1) => {
    const url = monitor1.Link
    console.log(url)
     try {
      const priceString =  await nightmare.goto(monitor1.Link)
                                         .wait(".a-price-whole")
                                         .evaluate(() => document.getElementsByClassName("a-price-whole").innerText)
                                         .end()
      console.log(priceString)
    } catch (e) {
      throw e
    }
  
  })

It returns undefined for all the prices. The class is called a-price-whole as shown here enter image description here

Why is this happening?

2

There are 2 answers

0
devilpreet On

Information is less, but I believe you are using same nightmare instance. Refer this answer

0
Sachin Som On

Scraping price by .a-offscreen class works for me.

 data.forEach(async (monitor1) => {
 const url = monitor1.Link
 console.log(url)
 try {
  const priceString =  await nightmare.goto(monitor1.Link)
                                     .wait(".a-offscreen")
                                     .evaluate(() => document.getElementsByClassName("a-offscreen").innerText)
                                     .end()
  console.log(priceString)
 } catch (e) {
   throw e
 }
})