Skip to content
Advertisement

How to add URL in the result of my analysis along with other data that get scrapped

I want to scrape many websites at once. So, I would prefer to have URL written in the result alongside with the other data that get scrapped. But I don’t know how. enter image description here

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://www.amazon.com/')


 await page.waitForTimeout( 10000 );
const localStorageData = await page.evaluate(() => {
let json = {};
for (let i = 0; i < localStorage.length; i++) {
  const key = localStorage.key(i);
  json[key] = localStorage.getItem(key);
}
return json;
});
const data = {};
for (let entry of Object.entries(data)) {
data[entry.key] = entry.value;
}
console.log(localStorageData)

await browser.close()
})()

Advertisement

Answer

You can simply add the URL you use to your JSON:

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  const url = "https://www.amazon.com/";
  await page.goto(url);

  await page.waitForTimeout(10000);
  const localStorageData = await page.evaluate((url) => {
    let json = {};
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      json[key] = localStorage.getItem(key);
    }
    return json;
  });
  localStorageData["url"] = url;
  const data = {};
  for (let entry of Object.entries(data)) {
    data[entry.key] = entry.value;
  }
  console.log(localStorageData);

  await browser.close();
})();


User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement