I'm trying to scrape prices from multiple pages using puppeteer. What i'm having trouble with, is to write a single JSON file with all the scraped data. The problem is that if i try to write the file with the variables from inside the async function
, i get an error saying that that variable hasn't been declared.
async function scrapeVMZ(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const [vmzel1] = await page.$x('//*[@id="__layout"]/div/div[1]/section/div/div/div[2]/div[2]/div[1]/div/div/div[2]/div/div[1]/span[2]');
const vmztxt1 = await vmzel1.getProperty('textContent');
const vmzRawTxt1 = await vmztxt1.jsonValue();
const [vmzel2] = await page.$x('//*[@id="__layout"]/div/div[1]/section/div/div/div[2]/div[2]/div[1]/div/div/div[2]/div/div[1]/span[4]/b');
const vmztxt2 = await vmzel2.getProperty('textContent');
const vmzRawTxt2 = await vmztxt2.jsonValue();
console.log({vmzRawTxt1, vmzRawTxt2});
const vmz01 = JSON.stringify(vmzRawTxt1);
const vmz02 = JSON.stringify(vmzRawTxt2);
console.log(vmz01, vmz02);
browser.close();
}
scrapeVMZ('https://www.vmzviagens.com.br/ingressos/orlando/walt-disney-orlando');
async function scrapeMB(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const [mbel1] = await page.$x('/html/body/section[3]/div/div/div[2]/div[1]/div/div[2]/a[1]/span[2]/span/div/div[2]/span');
const mbtxt1 = await mbel1.getProperty('textContent');
const mbRawTxt1 = await mbtxt1.jsonValue();
const [mbel2] = await page.$x('/html/body/section[3]/div/div/div[2]/div[1]/div/div[2]/a[1]/span[2]/span/div/div[4]/span');
const mbtxt2 = await mbel2.getProperty('textContent');
const mbRawTxt2 = await mbtxt2.jsonValue();
console.log({mbRawTxt1, mbRawTxt2});
const mb01 = JSON.stringify(mbRawTxt1);
const mb02 = JSON.stringify(mbRawTxt2);
console.log(mb01, mb02);
browser.close();
}
scrapeMB('https://www.ingressosmagicblue.com.br/produtos/?mpage=2');
How can i write a file, using the code above, to store inside my JSON file, the variables vmz01, vmz02
and mb01, mb02
, like the example below?
let abc = {
"MB": {
preco: mb01,
preco2: mb02
},
"VMZ": {
preco: vmz01,
preco2: vmz02
}
};