Json2csv overwrites columns
Hello, I have issue with json2csv, each line overwrites the previews one when I create excel file, I can see that in VS Code -> dataset, while creating the xlsx file, instead of creating multipole columns, it writes a new line instead of the previews line end I end up with a file with 1 line. Please help. Thank you
This is my code:
import { PlaywrightCrawler, Dataset } from 'crawlee';
import { writeFileSync } from 'fs';
import { parse } from 'json2csv';
const crawler = new PlaywrightCrawler({
requestHandler: async ({ page, request, enqueueLinks }) => {
console.log(Processing: ${request.url})
if (request.label === 'DETAIL') {
const branch_name = await page.locator('.store-details-right > h1 > span:nth-child(2)').textContent();
const street_name = await page.locator('.info-wrapper > .info:nth-child(1) > .info-value').textContent();
const results = {
branch_name: branch_name,
street_name: street_name,
}
const dataset = await Dataset.open('results-dataset');
await dataset.pushData(results);
const csv = parse(results);
writeFileSync('results.csv', csv);
} else {
// await page.waitForSelector('.more-details');
await enqueueLinks({
selector: '.more-details',
label: 'DETAIL',
})
}
},
headless: true,
});
await crawler.run(['https://www.example.com/]);
This is my code:
import { PlaywrightCrawler, Dataset } from 'crawlee';
import { writeFileSync } from 'fs';
import { parse } from 'json2csv';
const crawler = new PlaywrightCrawler({
requestHandler: async ({ page, request, enqueueLinks }) => {
console.log(Processing: ${request.url})
if (request.label === 'DETAIL') {
const branch_name = await page.locator('.store-details-right > h1 > span:nth-child(2)').textContent();
const street_name = await page.locator('.info-wrapper > .info:nth-child(1) > .info-value').textContent();
const results = {
branch_name: branch_name,
street_name: street_name,
}
const dataset = await Dataset.open('results-dataset');
await dataset.pushData(results);
const csv = parse(results);
writeFileSync('results.csv', csv);
} else {
// await page.waitForSelector('.more-details');
await enqueueLinks({
selector: '.more-details',
label: 'DETAIL',
})
}
},
headless: true,
});
await crawler.run(['https://www.example.com/]);