Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 剧作家;NodeJs-读取CSV并将数据推送到阵列_Javascript_Node.js_Arrays_Web Scraping_Playwright - Fatal编程技术网

Javascript 剧作家;NodeJs-读取CSV并将数据推送到阵列

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

我正在使用剧作家库进行网页抓取,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.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时,它是空的。这就是为什么它是空的。我尝试了所有操作,但结果仍然相同。任何其他记录建议?