i'm learning to web-scrapping with puppeteer, and when i'm trying to append some data into a json file when doing a for loop, y get a ][ separator between the objects, like this: json Data
here's the code:
const contratos = []
for (const contractHandles of contractsHandles) {
let name = 'Null'
let tipoContrato = 'Null'
let objContrato = 'Null'
let estadoContrato = 'Null'
let importeContrato = 0
let fechaContrato = 'Null'
// let importeParsed = 0
try {
// pass the single handle below
name = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdExpediente').innerText, contractHandles)
} catch (error) { console.log(error, 'Name Error') }
try {
tipoContrato = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdTipoContrato').innerText, contractHandles)
} catch (error) { console.log(error, 'Contrato Error') }
try {
objContrato = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdTipoContratoLicOC').innerText.replace(/[&/\\#,+()$~%.'":*?<>{}]/g, ''), contractHandles)
} catch (error) { console.log(error, 'Objetivo Contrato Error') }
try {
estadoContrato = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdEstado').innerText, contractHandles)
} catch (error) { console.log(error, 'Estado Contrato Error') }
try {
importeContrato = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdImporte').innerText, contractHandles)
} catch (error) { console.log(error, 'Importe Error') }
try {
fechaContrato = await page.evaluate(el => el.querySelector('#tableLicitacionesPerfilContratante tbody tr .tdFecha').innerText.replace(/[\n\r]/g, ' '), contractHandles)
} catch (error) { console.log(error, 'Fecha Error') }
if (name !== 'Null') {
contratos.push({ name, tipoContrato, objContrato, estadoContrato, importeContrato, fechaContrato })
fs.appendFile('results.json', JSON.stringify(contratos), 'utf8', () => console.log('File Writed Successfully'))
}
}
await page.waitForSelector('input[name="viewns_Z7_AVEQAI930GRPE02BR764FO30G0_:form1:siguienteLink"]', { visible: true })
const isDisabled = (await page.$('input[name="viewns_Z7_AVEQAI930GRPE02BR764FO30G0_:form1:siguienteLink"]')) === null
isBtnDisabled = isDisabled
if (!isDisabled) {
await Promise.all([
page.click('input[name="viewns_Z7_AVEQAI930GRPE02BR764FO30G0_:form1:siguienteLink"]'),
page.waitForNavigation({ waitUntil: 'load' })
])
console.log(contratos)
}
You can see the full code here githup repo
is not a big deal, because i just can replace ][ with a , with visual studio code, but i would like to know how can avoid it at the moment of appending the data to it, thank you so much.