I am using the playwright library for web scraping and URLs are stored in a CSV file. I am trying to read the CSV file and pick the URLs in an array to utilize in the scraping code.
Here is the code I wrote.
// Support
const csv = require('csv-parser');
const fs = require('fs');
// Array to store the URL.
var urls = [];
// This prints an empty array.
console.log(urls);
fs.createReadStream('sample.csv')
.pipe(csv())
.on('data', (row) => {
// Trying push the URL in the array
urls.push(row);
// This prints the values of URLs
console.log(urls);
})
.on('end', () => {
console.log('CSV file successfully processed');
});
// Here I don't see the URLs but an empty array.
console.log("URLS:" + urls);
In the method ".on('data'" the value gets pushed to the array and the console is also printing those, however, post-execution when I try to get the URLs from the array it returns an empty array.