Playwright & NodeJs -读取CSV并将数据推送到数组

igsr9ssn  于 2023-08-04  发布在  Node.js
关注(0)|答案(2)|浏览(111)

我使用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.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时,它返回一个空数组。

mpgws1up

mpgws1up1#

这个答案是假设链接是CSV文件中唯一的东西。

const { test } = require('@playwright/test');
const fs = require("fs");

// Reads the CSV file and saves it  
var links = fs.readFileSync('Path/to/csv')
    .toString() // convert Buffer to string
    .split('\n') // split string to lines
    .map(e => e.trim()) // remove white spaces for each line

// Start of for loop, to loop through csv file
for (const link of links) {
    // Normal test set up for playwright. adding the + link.toString to avoid duplicate test name error
    test('test for ' + link.toString(), async ({ page }) => {
        // First csv file item sent to console
        console.log(link);
        // Goes to that csv link item
        await page.goto(link);
        // Do whatever else you need
    });
}

字符串

ehxuflar

ehxuflar2#

下面的代码将从csv文件中提取数据并将其存储在String数组中。然后我们可以使用ArrayName[1],ArrayName[2]来获取实际值。

const fs = require('fs');

// Array to store the csv values.
var kpis : string[] = [];

kpis= fs.readFileSync('C:/Users/Public/Documents/KPIValues.csv')
.toString() // convert Buffer to string
.split('\n') // split string to lines
.map(e => e.trim()) ; // remove white spaces for each line;

console.log("All Data:" + kpis);
console.log("First row:"+ kpis[1]);

字符串

相关问题