Javascript 剧作家;NodeJs-读取CSV并将数据推送到阵列
我正在使用剧作家库进行网页抓取,URL存储在CSV文件中。我正在尝试读取CSV文件,并在一个数组中选择URL以用于抓取代码 这是我写的代码Javascript 剧作家;NodeJs-读取CSV并将数据推送到阵列,javascript,node.js,arrays,web-scraping,playwright,Javascript,Node.js,Arrays,Web Scraping,Playwright,我正在使用剧作家库进行网页抓取,URL存储在CSV文件中。我正在尝试读取CSV文件,并在一个数组中选择URL以用于抓取代码 这是我写的代码 // 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.createR
// 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);
在方法“.on('data')中该值被推送到数组中,控制台也在打印这些值,但是,在执行后,当我尝试从数组中获取URL时,它返回一个空数组。变量名是大写的,在最后执行console.log时,它是空的。这就是为什么它是空的。我尝试了所有操作,但结果仍然相同。任何其他记录建议?