Skip to content Skip to sidebar Skip to footer

How Do I Create A Csv File From All Of These Elements?

I am trying to get the text from both of these sections and turn it into a CSV list from puppeteer: item number: (Item 1055688) price: ( $16.59) here's what I tried but it doesn'

Solution 1:

I suppose the structure of your page is similar to this one

In this case you could use the following code:

// Find product descriptionsconst csv = await page.$$eval('.product_desc_txt', function(products){

    // Iterate over product descriptionslet csvLines = products.map(function(product){

        // Inside of each product find product SKU and its pricelet productId = product.querySelector(".custom-body-copy").innerText.trim();
        let productPrice = product.querySelector("span[data-wishlist-linkfee]").innerText.trim();

        // Fomrat them as a csv linereturn`${productId};${productPrice}`
    })

    // Join all lines into one filereturn csvLines.join("\n");

});

This code with the linked HTML structure produces this:

Item 1055688;$16.59 Item 1055688;$16.59 Item 1055688;$16.59 Item 1055688;$16.59


A more compact way to rewrite that with arrow functions would be the following (although I don't think it's very readable)

const csv = await page.$$eval('.product_desc_txt', products => products.map(product => product.querySelector(".custom-body-copy").innerText.trim() + ";" + product.querySelector("span[data-wishlist-linkfee]").innerText.trim()).join("\n"));

Post a Comment for "How Do I Create A Csv File From All Of These Elements?"