Nous sommes actuellement dans une conversation, voici l'histoire de…
- User
Assistant:
Prompt : Nous sommes actuellement dans une conversation, voici l'histoire de notre conversation sous la forme (SENDER : MESSAGE) (sachant que AI c'est toi USER c'est moi) : user: corrige
const browser = await puppeteer.launch({headless: "new"}); const page = await browser.newPage(); await page.goto(`https://www.ebay.fr/sch/i.html?_nkw=${item.replace(" ", "+")}&_ipg=240`); const itemInfoBased = await page.evaluate(() => { function parseCurrency(str) { const cleanedStr = str.replace(/\s*EUR$/, '').trim(); const normalizedStr = cleanedStr.replace(/\u00A0/g, '') const numberStr = normalizedStr.replace(',', '.'); const amount = parseFloat(numberStr); return amount; } let elements = document.querySelectorAll(".s-item__info") let arr = [] elements.forEach(e =>{ let priceItem = parseCurrency(e.querySelector(".s-item__price").textContent) if(priceItem !== null){ arr.push({ price: priceItem, }) } }) return JSON.stringify(arr) }) var data = JSON.parse(itemInfoBased); console.log(data) console.log(data.reduce((a, b) => a.price + b.price)) browser.close()ai: Your code snippet generally looks good, but there are a few improvements and corrections that can be made for better clarity and performance. I'll also correct some issues that may arise:
- Ensure that the
returnstatement in yourparseCurrencyfunction properly handles cases whereparseFloatfails. - The summation in the console log should correctly access the price properties of the array items.
- You might want to use
Promise.allif you plan to use multiple asynchronous operations for better performance. - Make sure to handle the case when there might be no prices available, as this could lead to errors.
Here's a revised version of your code:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ headless: "new" }); const page = await browser.newPage(); const item = "your_item_here"; // Replace with the appropriate item await page.goto(`https://www.ebay.fr/sch/i.html?_nkw=${encodeURIComponent(item)}&_ipg=240`); const itemInfoBased = await page.evaluate(() => { function parseCurrency(str) { const cleanedStr = str.replace(/\s*EUR$/, '').trim(); const normalizedStr = cleanedStr.replace(/\u00A0/g, ''); const numberStr = normalizedStr.replace(',', '.'); const amount = parseFloat(numberStr); return isNaN(amount) ? null : amount; // Return null if parsing fails } const elements = document.querySelectorAll(".s-item__info"); const arr = []; elements.forEach(e => { const priceItem = parseCurrency(e.querySelector(".s-item__price")?.textContent); if (priceItem !== null) { arr.push({ price: priceItem, }); } }); return arr; // Return the array instead of a JSON string }); console.log(itemInfoBased); const total = itemInfoBased.reduce((sum, current) => sum + current.price, 0); // Initialize sum to 0 console.log("Total price:", total); await browser.close(); })(); - Ensure that the
- Assistant
Pour calculer la moyenne des prix à partir de votre tableau
itemInfoBased, vous devez d'abord déterminer le nombre total d'éléments, puis diviser la somme des prix par ce nombre. Voici comment vous pouvez modifier votre code pour inclure le calcul de la moyenne :