Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Why my array gets only the last element from the browser?

I am new with scraping so I watched many videos and tried to understand some of it. So I am scraping answers from Stckoverflow by browsing every page that have the same keywords. It worked with the screenshots but what I really need is data in an array or json file. this is my function:

const getAnswerFromQuestion = async (website, query, page) => {
  console.log("Website", website);
  await page.goto(website, ["load", "domcontentloaded", "networkidle0"]);
  const popUp = (await page.$x("//button[@title='Dismiss']"))[0];
  if (popUp) await popUp.click();

  const generateId = shortid.generate();
  const screenshotPath = `${generateId}.png`;
  const acceptedAnswer = await page.$(".accepted-answer");
  const { data } = await axios.get(website);
    const $ = cheerio.load(data);
    const acceptedAnswer2 = $('.accepted-answer');
    const answers = [];
    acceptedAnswer2.find('.answercell.post-layout--right').each((i, element) => {
        const $element = $(element);
        const answer = {};
        answer.answerer = $element.find('.user-details > a').text();
        answer.content = $element.find('.s-prose.js-post-body').text();
        answers.push(answer);
        console.log(answers);
    });
  if (!acceptedAnswer) return;

  const pathToSaveScreenshot = `/answers/${query}/`;

  await createDir(pathToSaveScreenshot);

  await acceptedAnswer.screenshot({
    path: `.${pathToSaveScreenshot}${screenshotPath}`,
  });
};

I am trying to get the username of the answer and the content of the answer. It works but it adds the last answer data from the last browse of the site in the answers array.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!