Home > Back-end >  how to pass data from a webscraper into a javascript object
how to pass data from a webscraper into a javascript object

Time:12-18

I am using a web scraper to extract user reviews from google using an npm package called puppeteer. I have a function for every element I want to scrape these functions return the target text. I am able to extract the data and now I want to pass that data into a single Javascript object.

const puppeteer = require('puppeteer');

async function parseBody(page) {
  let reviewBody = [];

  const elements = await page.$$('.wiI7pd');
  if (elements && elements.length) {
    for (const el of elements) {
      const review = await el.evaluate((span) => span.textContent);
      reviewBody.push({ review });
    }
  }
  return reviewBody;
}

async function parseName(page) {
  let reviewerName = [];

  const elements = await page.$$('.d4r55');
  if (elements && elements.length) {
    for (const el of elements) {
      const name = await el.evaluate((span) => span.textContent);
      reviewerName.push({ name });
    }
  }
  return reviewerName;
}


(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.setViewport({
    width: 1500,
    height: 1200,
  });
  await page.goto(
    'https://www.google.com/maps/place/Luna Volcan'
  );
  const reviewBody = await parseBody(page);
  const reviewerName = await parseName(page);

  const result = [...reviewerName, ...reviewBody];

  console.log(result);
})();

By doing this I am able to log an array that looks like this: it lists all the names first and then the reviews.

[
  { name: ' Yuset Amado Calzadilla Cambara ' },
  { name: ' Diego Alexis ' },
  {
    review: 'Es una experiencia increíble este lugar, por las vistas, las atenc
            iones y las actividades que puedes realizar.'
  },
  {
    review: '¡Buenos días y gracias por dedicar tiempo a escribir esta maravill
            osa opinión!'
  },
]

but I'm trying to pass the data into an array of objects with the following structure, both name and review in a single object.

[
 {
  name: ' Yuset Amado Calzadilla Cambara '
  review: 'Es una experiencia increíble este lugar, por las vistas, las atenc
          iones y las actividades que puedes realizar. '
 }
 {
  name: ' Yuset Amado Calzadilla Cambara '
  review: '¡Buenos días y gracias por dedicar tiempo a escribir esta maravill
           osa opinión!'

 }
]

How could I go about this? Any help would be appreciated. I'm fairly new to JS.

CodePudding user response:

Since you want to use same index for both arrays, safest way is to use a for loop with same index for both. We can use object spread operator {...} to merge each object.

var reviewerName = [{
    name: ' Yuset Amado Calzadilla Cambara '
  },
  {
    name: ' Diego Alexis '
  },
]
var reviewBody = [{
    review: 'Es una experiencia increíble este lugar, por las vistas, las atenc iones y las actividades que puedes realizar.'
  },
  {
    review: '¡Buenos días y gracias por dedicar tiempo a escribir esta maravill osa opinión!'
  },
]

var result = []
for (var i = 0; i < reviewerName.length; i  ) {
  var o1 = reviewerName[i];
  var o2 = reviewBody[i];
  result.push({ ...o1, ...o2 })
}

console.log(result)

  • Related