仅将数组值存储到对象存储第一个值并不断重复该值

Storing Array Values to Objects Only Stores first value and keeps Repeating the Value Over And Over

我是 JavaScript 的新手,所以我现在没有很好的编程技能,所以我一直在研究一个 Web Scraper,它 returns 一个包含名称、帖子、简历等的数组,如下所示:

let infoOfPost = await newTab(browser, page);

所以 infoOfPost 是一个数组,它的值在变化,因为它在循环中被调用,我可以在控制台中看到它每次都有新的 bio、posts、followers 等值。 但是当我将这个值推送到一个对象时,该对象仅在第一次循环 运行 时存储初始值,并且在每次下一次迭代中它只保持显示相同的值并且不会覆盖先前的值我将数组存储在对象为:

    accountInfoObject.displayName =infoOfPost[0];
    accountInfoObject.posts = infoOfPost[1];
    accountInfoObject.followers=infoOfPost[2];
    accountInfoObject.following =infoOfPost[3];
    accountInfoObject.fullName = infoOfPost[4];
    accountInfoObject.about =infoOfPost[5];
    accountsInformation.push(accountInfoObject);
    await objectsCsv(accountsInformation);

我现在看到的是这样的:

[
 {
   accountUrl: 'https://www.example.com/xyz.hij/',
   displayName: 'saharpalmer',
   posts: '368',
   followers: '2,640',
   following: '510',
   fullName: 'Sahar Intuitive Life Mentor',
   about: '30-year Experience: I help you shift your mindsetGet back on track quickly Fulfil your purpose & live your best life'
  }
]

我想看到的是,我的所有其他条目都在其后跟一个逗号,并使其成为对象数组而不是单个对象数组。 目前我只看到 this Once 和 this Array of single Object 不断重复。此外,我将此对象推送到一个数组并将其写入 Csv 文件,该文件也包含此对象一次又一次地重复,如下所示:

about   accountUrl  displayName posts   followers   following   fullName
30-year Experience: I help you shift your mindset??Get back on track quickly??Fulfil your purpose & live your best life??'  https://www.example.com/being.darsh/    saharpalmer 368 2640    510 Sahar
30-year Experience: I help you shift your mindset??Get back on track quickly??Fulfil your purpose & live your best life??'  https://www.example.com/being.darsh/    saharpalmer 368 2640    510 Sahar

对象和数组声明如下:

let accountsInformation = [];
let accountInfoObject = new Object();

完整代码是: 我们从中获取数组的文件是:

let accountsInformation = [];
let accountInfoObject = new Object();
async function scrapingPosts(browser, page) {
  readCsvFile(urlsToVisit);
  for (let x = 0; x < urlsToVisit.length; x++) {
    secondaryUrl = urlsToVisit[x];
    await page.waitFor(10000);
    await page
      .goto(`${secondaryUrl}`, {
        waitUntil: "domcontentloaded",
      })
      .catch((e) => {});
    await page.waitForSelector("article >div.EZdmt:nth-child(2)",
      5000);
    for (let i = 1; i < 5; i++) {
      await page.waitFor(5000);
      //  this loops goes through all 3 posts of each container;
      for (let j = 1; j <= 3; j++) {
        // opening the modal means clicking on post i and j will 
        increment and we will keep moving to next post 1 by 1
        await page.click(
          `div.EZdmt > div > div > div:nth-child(${i}) > div:nth-child(${j})`);
        let url = await urlOfIds(page, urlsAddress);
        await page.waitFor(5000);
        let infoOfPost = await newTab(browser, page);
        accountInfoObject.accountUrl = url;
        accountInfoObject.displayName = infoOfPost[0];
        accountInfoObject.posts = infoOfPost[1];
        accountInfoObject.followers = infoOfPost[2];
        accountInfoObject.following = infoOfPost[3];
        accountInfoObject.fullName = infoOfPost[4];
        accountInfoObject.about = infoOfPost[5];
        await page.waitFor(10000);
        accountsInformation.push(accountInfoObject);
        console.log(accountsInformation);
        await objectsCsv(accountsInformation);
        // Modal Closes here process repeats till the loop condition is unsatisfied
        await page.click(
          "body > div._2dDPU.QPGbb.CkGkG > div.qF0y9._4EzTm.BI4qX.qJPeX.fm1AK.TxciK.yiMZG >button.wpO6b");
        await page.waitFor(20000);
      }
    }
  }
  await browser.close();
}

infoOfPosts 来自的文件是:

let evalSelector;
const selectorData = [];
async function newTab(browser, page) {
  await page.keyboard.down("Control");
  await page.click("span.Jv7Aj.mArmR.MqpiF");
  await page.keyboard.up("Control");
  await page.waitForTimeout(1000);
  const newPage = (await browser.pages())[1];
  await newPage.waitForNavigation("#react-root");
  await newPage.waitFor(20000);
  evalSelector = await selectorEvaluation(newPage, titleSelector);
  selectorData.push(evalSelector);
  evalSelector = await selectorEvaluation(newPage, noPostSelector);
  selectorData.push(evalSelector);
  evalSelector = await selectorEvaluation(newPage,
    noOfFollowersSelector);
  selectorData.push(evalSelector);
  evalSelector = await selectorEvaluation(newPage,
    noOfFollowingSelector);
  selectorData.push(evalSelector);
  evalSelector = await selectorEvaluation(newPage,
    displayNameSelector);
  selectorData.push(evalSelector);
  evalSelector = await selectorEvaluation(newPage, aboutSelector);
  selectorData.push(evalSelector);
  console.log(selectorData);
  await newPage.waitFor(5000);
  await newPage.close();
  return selectorData;
}
module.exports = newTab;

非常感谢任何帮助。提前致谢。 荣誉!!

你有两个问题。

  1. 您每次 accountsInformation.push(accountInfoObject);.
  2. 都在重复使用相同的 accountInfoObject
let accountsInformation = [];
async function scrapingPosts(browser, page) {
  readCsvFile(urlsToVisit);
  for (let x = 0; x < urlsToVisit.length; x++) {
    secondaryUrl = urlsToVisit[x];
    await page.waitFor(10000);
    await page
      .goto(`${secondaryUrl}`, {
        waitUntil: "domcontentloaded",
      })
      .catch((e) => {});
    await page.waitForSelector("article >div.EZdmt:nth-child(2)",
      5000);
    for (let i = 1; i < 5; i++) {
      await page.waitFor(5000);
      //  this loops goes through all 3 posts of each container;
      for (let j = 1; j <= 3; j++) {
        // opening the modal means clicking on post i and j will 
        increment and we will keep moving to next post 1 by 1
        await page.click(
          `div.EZdmt > div > div > div:nth-child(${i}) > div:nth-child(${j})`);
        let url = await urlOfIds(page, urlsAddress);
        await page.waitFor(5000);
        let infoOfPost = await newTab(browser, page);
        let accountInfoObject = {
            accountUrl: url,
            displayName: infoOfPost[0],
            posts: infoOfPost[1],
            followers: infoOfPost[2],
            following: infoOfPost[3],
            fullName: infoOfPost[4],
            about: infoOfPost[5]
        };
        await page.waitFor(10000);
        accountsInformation.push(accountInfoObject);
        console.log(accountsInformation);
        await objectsCsv(accountsInformation);
        // Modal Closes here process repeats till the loop condition is unsatisfied
        await page.click(
          "body > div._2dDPU.QPGbb.CkGkG > div.qF0y9._4EzTm.BI4qX.qJPeX.fm1AK.TxciK.yiMZG >button.wpO6b");
        await page.waitFor(20000);
      }
    }
  }
  await browser.close();
}
  1. 在将新数据推送到 newTab() 之前,您没有清除 selectorData。您应该将其设为局部变量。因此,当 scrapingPosts() 使用元素 0 到 5 时,它们来自第一个被提取的 post。
async function newTab(browser, page) {
  await page.keyboard.down("Control");
  await page.click("span.Jv7Aj.mArmR.MqpiF");
  await page.keyboard.up("Control");
  await page.waitForTimeout(1000);
  const newPage = (await browser.pages())[1];
  await newPage.waitForNavigation("#react-root");
  await newPage.waitFor(20000);
  const selectorData = [];
  for (let selector of [titleSelector, noPostSelector, noOfFollowersSelector, noOfFollowingSelector, displayNameSelector, aboutSelector]) {
    let evalSelector = await selectorEvaluation(newPage, selector);
    selectorData.push(evalSelector);
  }
  console.log(selectorData);
  await newPage.waitFor(5000);
  await newPage.close();
  return selectorData;
}
module.exports = newTab;